package com.zhehekeji.opsys.api.service.manager;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.persistence.Query;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhehekeji.opsys.api.code.LuckyVoteResultCode;
import com.zhehekeji.opsys.api.common.util.DateFormatUtil;
import com.zhehekeji.opsys.api.conf.Config;
import com.zhehekeji.opsys.api.enm.EpgType;
import com.zhehekeji.opsys.api.enm.VoteTimesValidity;
import com.zhehekeji.opsys.api.enm.VoteUserType;
import com.zhehekeji.opsys.api.entity.TVoteGame;
import com.zhehekeji.opsys.api.entity.TVotePlayer;
import com.zhehekeji.opsys.api.entity.TVoteQrcode;
import com.zhehekeji.opsys.api.entity.TVoteQrcodeDetail;
import com.zhehekeji.opsys.api.entity.TVoteRecord;
import com.zhehekeji.opsys.api.entity.TVoteRecordDay;
import com.zhehekeji.opsys.api.entity.TVoteRegion;
import com.zhehekeji.opsys.api.entity.TVoteSummary;
import com.zhehekeji.opsys.api.entity.TVoteUser;
import com.zhehekeji.opsys.api.resp.GetDayVoteRecordCountResp;
import com.zhehekeji.opsys.api.resp.GetPeriodVoteRecordCountResp;
import com.zhehekeji.opsys.api.resp.GetVoteListResp;
import com.zhehekeji.opsys.api.resp.GetVotePlayerCountOnlyResp;
import com.zhehekeji.opsys.api.resp.GetVotePlayerListOnlyResp;
import com.zhehekeji.opsys.api.resp.GetVotePlayerListResp;
import com.zhehekeji.opsys.api.resp.GetVotePlayerResp;
import com.zhehekeji.opsys.api.resp.GetVoteQrCodeDetailResp;
import com.zhehekeji.opsys.api.resp.GetVoteRecordListResp;
import com.zhehekeji.opsys.api.resp.GetVoteRegionDetailResp;
import com.zhehekeji.opsys.api.resp.GetVoteRegionListResp;
import com.zhehekeji.opsys.api.resp.GetVoteResp;
import com.zhehekeji.opsys.api.resp.LuckyVoteResp;
import com.zhehekeji.opsys.api.resp.VerifyInviteCodeResp;
import com.zhehekeji.opsys.api.service.CommonService;
import com.zhehekeji.opsys.api.service.validate.ValidateRets;
import com.zhehekeji.opsys.api.service.validate.VoteGameValidate;
import com.zhehekeji.opsys.api.service.validate.VotePlayerValidate;
import com.zhehekeji.opsys.api.service.validate.args.VoteGameValidateArgs;
import com.zhehekeji.opsys.api.service.validate.args.VotePlayerValidateArgs;

/**
 * @ClassName VoteManagerService
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2016-3-16   下午2:02:42
 */
@Service
public class VoteManagerService extends CommonService {
	
	private final static Logger logger = LoggerFactory.getLogger(VoteManagerService.class);
	
	@Autowired
	private VoteGameValidate voteGameValidate;
	@Autowired
	private VotePlayerValidate votePlayerValidate;
	
	@Autowired
	private Config config;
	
	private String getVotePlayerSelectSql(){
		String tmpSql = "select o.ID as id,o.CODE as code,o.NAME as name, o.PLAYER_NO as playerNo,"
				+ "o.REL_CODE as relCode,o.REL_CODEHD as relCodehd,o.AVATAR as avatar, o.AVATARHD as avatarhd,"
				+ "o.VOTES as votes,o.VOTES_OTHER as votesOther,o.VOTES_TOTAL as votesTotal, o.VOTE_ID as voteId,"
				+ "o.PROFILE as profile,o.SEQUENCE as sequence,o.CREATE_TIME as createTime, o.UPDATE_TIME as updateTime,"
				+ "o.STATUS as status,o.CP_CODE as cpCode,o.REGION_ID as regionId, o.VOTE_TIMES as voteTimes,"
				+ "o.VOTE_TIMES_HD as voteTimesHd,o.VOTE_TIMES_4K as voteTimes4k,o.VERCTRL as verctrl, "
				+ "o.START_TIME as startTime,o.END_TIME as endTime"
				+ " from t_vote_player o ";
		return tmpSql;
	}
	
	private String getVoteWeekChampSelectSql(){
		String tmpSql = "select o.ID as id,o.CODE as code,o.NAME as name, o.PLAYER_NO as playerNo,"
				+ "o.REL_CODE as relCode,o.REL_CODEHD as relCodehd,o.AVATAR as avatar, o.AVATARHD as avatarhd,"
				+ "o.VOTES as votes,o.GAME_SITE as gameSite,o.ANNOUNCE_TIME as announceTime, o.ANNOUNCE_STATUS as announceStatus,"
				+ "o.VOTE_ID as voteId,o.PROFILE as profile,o.CREATE_TIME as createTime, o.UPDATE_TIME as updateTime,"
				+ "o.STATUS as status,o.CP_CODE as cpCode,o.REGION_ID as regionId"
				+ " from t_vote_week_champ o ";
		return tmpSql;
	}
	
	private String getVoteRegionSelectSql(){
		String tmpSql = "select o.ID as id,o.CODE as code,o.NAME as name, o.ORIGINAL_CODE as originalCode,"
				+ "o.SEQUENCE as sequence,o.CREATE_TIME as createTime,o.STATUS as status, o.CP_CODE as cpCode,"
				+ "o.PARENT_ID as parentId,o.VOTE_TIMES as voteTimes,o.VOTE_TIMES_HD as voteTimesHd, o.VOTE_TIMES_4K as voteTimes4k,"
				+ "o.START_TIME as startTime,o.END_TIME as endTime"
				+ " from t_vote_region o ";
		return tmpSql;
	}
	
	private String getVoteUserSelectSql(){
		String tmpSql = "select o.ID as id,o.USER_ID as userId,o.VOTE_ID as voteId,o.PRODUCT_CODES as productCodes,o.PRODUCT_VOTES as productVotes,"
				+ "o.CAN_VOTES as canVotes,o.REMAIN_VOTES as remainVotes,o.CREATE_TIME as createTime,o.UPDATE_TIME as updateTime,"
				+ "o.STATUS as status,o.CP_CODE as cpCode,o.VOTE_BASE as voteBase,o.VOTE_QRCODE as voteQrcode,"
				+ "o.EPG_TYPE as epgType from t_vote_user o ";
		return tmpSql;
	}
	
	private TVoteUser getTVoteUser(TVoteGame tmpTVoteGame, String userID) throws SQLException{
		TVoteUser tmpTVoteUser = null;
		String tmpSql = getVoteUserSelectSql() + " where o.VOTE_ID=" + tmpTVoteGame.getId() + " and o.USER_ID=?";
		List<Map<String, Object>> tmpListMap = entityDao.getScrollDataBySql(tmpSql, new Object[]{userID}, -1, -1);
		BigInteger tmpBig;
		if (tmpListMap != null && !tmpListMap.isEmpty()){
			Map<String, Object> tmpMap = tmpListMap.get(0);
			tmpTVoteUser = new TVoteUser();
			tmpTVoteUser.setCanVotes((Integer)tmpMap.get("canVotes"));
			tmpBig = (BigInteger)tmpMap.get("cpCode");
			if (tmpBig != null){
				tmpTVoteUser.setCpCode(tmpBig.longValue());
			}
			tmpTVoteUser.setCreateTime((Date)tmpMap.get("createTime"));
			tmpTVoteUser.setEpgType((Integer)tmpMap.get("epgType"));
			tmpBig = (BigInteger)tmpMap.get("id");
			if (tmpBig != null){
				tmpTVoteUser.setId(tmpBig.longValue());
			}
			tmpTVoteUser.setProductCodes((String)tmpMap.get("productCodes"));
			tmpTVoteUser.setProductVotes((String)tmpMap.get("productVotes"));
			tmpTVoteUser.setRemainVotes((Integer)tmpMap.get("remainVotes"));
			tmpTVoteUser.setStatus((Integer)tmpMap.get("status"));
			tmpTVoteUser.setTvoteGame(tmpTVoteGame);
			tmpTVoteUser.setUpdateTime((Date)tmpMap.get("updateTime"));
			tmpTVoteUser.setUserId((String)tmpMap.get("userId"));
			tmpTVoteUser.setVoteBase((Integer)tmpMap.get("voteBase"));
			tmpTVoteUser.setVoteQrcode((Integer)tmpMap.get("voteQrcode"));
		}
		return tmpTVoteUser;
	}
	
	public GetVotePlayerListResp getVotePlayerList(String voteCode, String regionCode, int status, String sortName,
                                                   String sortOrder, int firstIndex, int maxResult){
		GetVotePlayerListResp resp = new GetVotePlayerListResp();
		String resultDesc = String.format("投票活动[%s]获取参赛选手成功！", voteCode);
		try{
			if (StringUtils.isBlank(voteCode)){
				resultDesc = String.format("投票活动[%s]获取参赛选手失败，编码非法！", voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("投票活动[%s]获取参赛选手失败，投票活动不存在！", voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.VOTE_DOES_NOT_EXIST);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			
			TVoteRegion tmpTVoteRegion = null;
			String tmpSql;
			List<Map<String, Object>> tmpListMap;
			BigInteger tmpBig;
			if (!StringUtils.isBlank(regionCode)){
				tmpSql = "select o.ID as id,o.CODE as code,o.NAME as name, o.STATUS as status"
						+ " from t_vote_region o where o.CODE=?";
				tmpListMap = entityDao.getScrollDataBySql(tmpSql, new Object[]{regionCode}, -1, -1);
				if (tmpListMap == null || tmpListMap.isEmpty()){
					resultDesc = String.format("投票活动[%s]获取区域[%s]参赛选手失败，区域编码非法！", voteCode, regionCode);
					logger.error(resultDesc);
					resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
					resp.setResultDesc(resultDesc);
					return resp;
				}
				Map<String, Object> tmpMap = tmpListMap.get(0);
				
				tmpTVoteRegion = new TVoteRegion();
				tmpBig = (BigInteger)tmpMap.get("id");
				tmpTVoteRegion.setId(tmpBig.longValue());
				tmpTVoteRegion.setCode((String)tmpMap.get("code"));
				tmpTVoteRegion.setName((String)tmpMap.get("name"));
				tmpTVoteRegion.setStatus((Integer)tmpMap.get("status"));
				
				if (tmpTVoteRegion.getStatus() == 0){
					resultDesc = String.format("投票活动[%s]获取区域[%s]参赛选手失败，区域已停用！", voteCode, regionCode);
					logger.error(resultDesc);
					resp.setResultCode(LuckyVoteResultCode.REGION_HAS_BEEN_STOPPED);
					resp.setResultDesc(resultDesc);
					return resp;
				}
				if (status == 0){
					tmpSql = "select count(o.id) as count from TVotePlayer o where o.tvoteGame.id=? and o.status=1 and o.tvoteRegion.id=?";
				}else if (status == 1){
					tmpSql = "select count(o.id) as count from TVotePlayer o where o.tvoteGame.id=? and o.status=0 and o.tvoteRegion.id=?";
				}else{
					tmpSql = "select count(o.id) as count from TVotePlayer o where o.tvoteGame.id=? and o.tvoteRegion.id=?";
				}
				
				tmpListMap = entityDao.getScrollDataByHql(tmpSql, new Object[]{tmpTVoteGame.getId(), tmpTVoteRegion.getId()}, -1, -1);
			}else{
				if (status == 0){
					tmpSql = "select count(o.id) as count from TVotePlayer o where o.tvoteGame.id=? and o.status=1";
				}else if (status == 1){
					tmpSql = "select count(o.id) as count from TVotePlayer o where o.tvoteGame.id=? and o.status=0";
				}else{
					tmpSql = "select count(o.id) as count from TVotePlayer o where o.tvoteGame.id=?";
				}
				
				tmpListMap = entityDao.getScrollDataByHql(tmpSql, new Object[]{tmpTVoteGame.getId()}, -1, -1);
			}
			
			int tmpIntCount = 0;
			if (tmpListMap != null && !tmpListMap.isEmpty()){
				Map<String, Object> tmpMapCount = tmpListMap.get(0);
				Long tmpLongCount = (Long) tmpMapCount.get("count");
				tmpIntCount = tmpLongCount.intValue();
			}
			if (tmpIntCount <= 0) return resp;
			
			tmpSql = getVotePlayerSelectSql() + " where o.VOTE_ID=" + tmpTVoteGame.getId();
			
			/*String orderBy = " order by o.SEQUENCE desc";
			String tmpSortName = "o.VOTES";
			String tmpSortOrder = "desc";
			if (!StringUtils.isBlank(sortName)){
				if (sortName.equals("createTime")){
					tmpSortName = "o.CREATE_TIME";
				}else if(sortName.equals("playerNo")){
					tmpSortName = "o.PLAYER_NO";
				}
			}
			if (!StringUtils.isBlank(sortOrder)){
				if (sortOrder.equals("asc")){
					tmpSortOrder = "asc";
				}
			}
			orderBy = orderBy + "," + tmpSortName + " " + tmpSortOrder;*/
			
			String orderBy = " order by ";
			String tmpSortName = "o.VOTES";
			String tmpSortOrder = "desc";
			if (!StringUtils.isBlank(sortName)){
				if (sortName.equals("createTime")){
					tmpSortName = "o.CREATE_TIME";
				}else if(sortName.equals("playerNo")){
					tmpSortName = "o.PLAYER_NO";
				}else if(sortName.equals("sequence")){
					tmpSortName = "o.SEQUENCE";
				}
			}
			if (!StringUtils.isBlank(sortOrder)){
				if (sortOrder.equals("asc")){
					tmpSortOrder = "asc";
				}
			}
			orderBy = orderBy + tmpSortName + " " + tmpSortOrder;
			
			String tmpWhereJpql = "";
			if (!StringUtils.isBlank(regionCode)){
				if (status == 0){
					tmpWhereJpql = " and o.STATUS=1 and o.REGION_ID=" + tmpTVoteRegion.getId();
				}else if (status == 1){
					tmpWhereJpql = " and o.STATUS=0 and o.REGION_ID=" + tmpTVoteRegion.getId();
				}else{
					tmpWhereJpql = " and o.REGION_ID=" + tmpTVoteRegion.getId();
				}
			}else{
				if (status == 0){
					tmpWhereJpql = " and o.STATUS=1";
				}else if (status == 1){
					tmpWhereJpql = " and o.STATUS=0";
				}
			}
			tmpSql = tmpSql + tmpWhereJpql + orderBy;
			tmpListMap = entityDao.getScrollDataBySql(tmpSql, null, firstIndex, maxResult);
			if (tmpListMap == null || tmpListMap.isEmpty()){
				return resp;
			}
			
			List<GetVotePlayerListResp.VotePlayerInfo> tmpListVotePlayerInfo = new ArrayList<GetVotePlayerListResp.VotePlayerInfo>();
			GetVotePlayerListResp.VotePlayerInfo tmpVotePlayerInfo;
			String tmpStr;
			tmpSql = "select o.CODE as code,o.NAME as name from t_vote_region o where o.ID=%s";
			List<Map<String, Object>> tmpListRgMap;
			Map<String, Object> tmpRgMap;
			Map<Long, TVoteRegion> tmpVrMap = new HashMap<Long, TVoteRegion>();
			Long tmpRegionId;
			TVoteRegion tmpTVoteRegion2;
			for (Map<String, Object> tmpMap : tmpListMap){
				tmpVotePlayerInfo = new GetVotePlayerListResp.VotePlayerInfo();
				tmpStr = (String) tmpMap.get("avatar");
				if (!StringUtils.isBlank(tmpStr)){
					tmpVotePlayerInfo.setAvatar(getHttpUrl(tmpStr));
				}
				tmpStr = (String) tmpMap.get("avatarhd");
				if (!StringUtils.isBlank(tmpStr)){
					tmpVotePlayerInfo.setAvatarhd(getHttpUrl(tmpStr));
				}
				tmpVotePlayerInfo.setCode((String) tmpMap.get("code"));
				tmpVotePlayerInfo.setName((String) tmpMap.get("name"));
				tmpVotePlayerInfo.setProfile((String) tmpMap.get("profile"));
				tmpVotePlayerInfo.setRelCode((String) tmpMap.get("relCode"));
				tmpVotePlayerInfo.setRelCodehd((String) tmpMap.get("relCodehd"));
				tmpVotePlayerInfo.setVotes((Integer) tmpMap.get("votes"));
				tmpVotePlayerInfo.setVotesOther((Integer) tmpMap.get("votesOther"));
				tmpVotePlayerInfo.setVotesTotal((Integer) tmpMap.get("votesTotal"));
				
				if (tmpTVoteRegion != null){
					tmpVotePlayerInfo.setRegionCode(tmpTVoteRegion.getCode());
					tmpVotePlayerInfo.setRegionName(tmpTVoteRegion.getName());
				}else{
					tmpBig = (BigInteger) tmpMap.get("regionId");
					if (tmpBig != null){
						tmpRegionId = tmpBig.longValue();
						tmpTVoteRegion2 = tmpVrMap.get(tmpRegionId);
						if (tmpTVoteRegion2 == null){
							tmpListRgMap = entityDao.getScrollDataBySql(String.format(tmpSql, tmpRegionId), null, -1, -1);
							if (tmpListRgMap != null && !tmpListRgMap.isEmpty()){
								tmpRgMap = tmpListRgMap.get(0);
								tmpTVoteRegion2 = new TVoteRegion();
								tmpTVoteRegion2.setCode((String) tmpRgMap.get("code"));
								tmpTVoteRegion2.setName((String) tmpRgMap.get("name"));
								tmpVrMap.put(tmpRegionId, tmpTVoteRegion2);
							}
						}
						if (tmpTVoteRegion2 != null){
							tmpVotePlayerInfo.setRegionCode(tmpTVoteRegion2.getCode());
							tmpVotePlayerInfo.setRegionName(tmpTVoteRegion2.getName());
						}
						
					}
				}
				
				tmpVotePlayerInfo.setPlayerNo((String) tmpMap.get("playerNo"));
				tmpVotePlayerInfo.setStatus((Integer) tmpMap.get("status"));
				tmpVotePlayerInfo.setStartTime(DateFormatUtil.format((Date) tmpMap.get("startTime")));
				tmpVotePlayerInfo.setEndTime(DateFormatUtil.format((Date) tmpMap.get("endTime")));
				tmpListVotePlayerInfo.add(tmpVotePlayerInfo);
			}
			
			resp.setVotePlayerInfos(tmpListVotePlayerInfo);
			resp.setRecordSize(tmpIntCount);
			logger.info(resultDesc);
		}catch(Exception e){
			resultDesc = String.format("投票活动[%s]获取参赛选手失败，程序异常！", voteCode);
			logger.error(resultDesc, e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
	}
	
	public GetVotePlayerCountOnlyResp getVotePlayerCountOnly(String voteCode, String regionCode, int status){
		GetVotePlayerCountOnlyResp resp = new GetVotePlayerCountOnlyResp();
		String resultDesc = String.format("投票活动[%s]获取参赛选手数量成功！", voteCode);
		try{
			if (StringUtils.isBlank(voteCode)){
				resultDesc = String.format("投票活动[%s]获取参赛选手数量失败，编码非法！", voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("投票活动[%s]获取参赛选手数量失败，投票活动不存在！", voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.VOTE_DOES_NOT_EXIST);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			
			TVoteRegion tmpTVoteRegion = null;
			String tmpSql;
			List<Map<String, Object>> tmpListMap;
			BigInteger tmpBig;
			if (!StringUtils.isBlank(regionCode)){
				tmpSql = "select o.ID as id,o.CODE as code,o.NAME as name, o.STATUS as status"
						+ " from t_vote_region o where o.CODE=?";
				tmpListMap = entityDao.getScrollDataBySql(tmpSql, new Object[]{regionCode}, -1, -1);
				if (tmpListMap == null || tmpListMap.isEmpty()){
					resultDesc = String.format("投票活动[%s]获取区域[%s]参赛选手失败，区域编码非法！", voteCode, regionCode);
					logger.error(resultDesc);
					resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
					resp.setResultDesc(resultDesc);
					return resp;
				}
				Map<String, Object> tmpMap = tmpListMap.get(0);
				
				tmpTVoteRegion = new TVoteRegion();
				tmpBig = (BigInteger)tmpMap.get("id");
				tmpTVoteRegion.setId(tmpBig.longValue());
				tmpTVoteRegion.setCode((String)tmpMap.get("code"));
				tmpTVoteRegion.setName((String)tmpMap.get("name"));
				tmpTVoteRegion.setStatus((Integer)tmpMap.get("status"));
				
				if (tmpTVoteRegion.getStatus() == 0){
					resultDesc = String.format("投票活动[%s]获取区域[%s]参赛选手失败，区域已停用！", voteCode, regionCode);
					logger.error(resultDesc);
					resp.setResultCode(LuckyVoteResultCode.REGION_HAS_BEEN_STOPPED);
					resp.setResultDesc(resultDesc);
					return resp;
				}
				if (status == 0){
					tmpSql = "select count(o.id) as count from TVotePlayer o where o.tvoteGame.id=? and o.status=1 and o.tvoteRegion.id=?";
				}else if (status == 1){
					tmpSql = "select count(o.id) as count from TVotePlayer o where o.tvoteGame.id=? and o.status=0 and o.tvoteRegion.id=?";
				}else{
					tmpSql = "select count(o.id) as count from TVotePlayer o where o.tvoteGame.id=? and o.tvoteRegion.id=?";
				}
				
				tmpListMap = entityDao.getScrollDataByHql(tmpSql, new Object[]{tmpTVoteGame.getId(), tmpTVoteRegion.getId()}, -1, -1);
			}else{
				if (status == 0){
					tmpSql = "select count(o.id) as count from TVotePlayer o where o.tvoteGame.id=? and o.status=1";
				}else if (status == 1){
					tmpSql = "select count(o.id) as count from TVotePlayer o where o.tvoteGame.id=? and o.status=0";
				}else{
					tmpSql = "select count(o.id) as count from TVotePlayer o where o.tvoteGame.id=?";
				}
				
				tmpListMap = entityDao.getScrollDataByHql(tmpSql, new Object[]{tmpTVoteGame.getId()}, -1, -1);
			}
			
			int tmpIntCount = 0;
			if (tmpListMap != null && !tmpListMap.isEmpty()){
				Map<String, Object> tmpMapCount = tmpListMap.get(0);
				Long tmpLongCount = (Long) tmpMapCount.get("count");
				tmpIntCount = tmpLongCount.intValue();
			}
			if (tmpIntCount <= 0) {
				resp.setRecordSize(0);
			}else{
				resp.setRecordSize(tmpIntCount);
			}
			
			logger.info(resultDesc);
		}catch(Exception e){
			resultDesc = String.format("投票活动[%s]获取参赛选手数量失败，程序异常！", voteCode);
			logger.error(resultDesc, e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
	}
	
	public GetVotePlayerListOnlyResp getVotePlayerListOnly(String voteCode, String regionCode, int status, String sortName, 
			String sortOrder, int firstIndex, int maxResult){
		GetVotePlayerListOnlyResp resp = new GetVotePlayerListOnlyResp();
		String resultDesc = String.format("投票活动[%s]获取参赛选手数量成功！", voteCode);
		try{
			if (StringUtils.isBlank(voteCode)){
				resultDesc = String.format("投票活动[%s]获取参赛选手失败，编码非法！", voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("投票活动[%s]获取参赛选手失败，投票活动不存在！", voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.VOTE_DOES_NOT_EXIST);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			
			TVoteRegion tmpTVoteRegion = null;
			String tmpSql;
			List<Map<String, Object>> tmpListMap;
			BigInteger tmpBig;
			if (!StringUtils.isBlank(regionCode)){
				tmpSql = "select o.ID as id,o.CODE as code,o.NAME as name, o.STATUS as status"
						+ " from t_vote_region o where o.CODE=?";
				tmpListMap = entityDao.getScrollDataBySql(tmpSql, new Object[]{regionCode}, -1, -1);
				if (tmpListMap == null || tmpListMap.isEmpty()){
					resultDesc = String.format("投票活动[%s]获取区域[%s]参赛选手失败，区域编码非法！", voteCode, regionCode);
					logger.error(resultDesc);
					resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
					resp.setResultDesc(resultDesc);
					return resp;
				}
				Map<String, Object> tmpMap = tmpListMap.get(0);
				
				tmpTVoteRegion = new TVoteRegion();
				tmpBig = (BigInteger)tmpMap.get("id");
				tmpTVoteRegion.setId(tmpBig.longValue());
				tmpTVoteRegion.setCode((String)tmpMap.get("code"));
				tmpTVoteRegion.setName((String)tmpMap.get("name"));
				tmpTVoteRegion.setStatus((Integer)tmpMap.get("status"));
				
				if (tmpTVoteRegion.getStatus() == 0){
					resultDesc = String.format("投票活动[%s]获取区域[%s]参赛选手失败，区域已停用！", voteCode, regionCode);
					logger.error(resultDesc);
					resp.setResultCode(LuckyVoteResultCode.REGION_HAS_BEEN_STOPPED);
					resp.setResultDesc(resultDesc);
					return resp;
				}
			}
			
			tmpSql = getVotePlayerSelectSql() + " where o.VOTE_ID=" + tmpTVoteGame.getId();
			
			String orderBy = " order by ";
			String tmpSortName = "o.VOTES";
			String tmpSortOrder = "desc";
			if (!StringUtils.isBlank(sortName)){
				if (sortName.equals("createTime")){
					tmpSortName = "o.CREATE_TIME";
				}else if(sortName.equals("playerNo")){
					tmpSortName = "o.PLAYER_NO";
				}else if(sortName.equals("sequence")){
					tmpSortName = "o.SEQUENCE";
				}
			}
			if (!StringUtils.isBlank(sortOrder)){
				if (sortOrder.equals("asc")){
					tmpSortOrder = "asc";
				}
			}
			orderBy = orderBy + tmpSortName + " " + tmpSortOrder;
			
			String tmpWhereJpql = "";
			if (!StringUtils.isBlank(regionCode)){
				if (status == 0){
					tmpWhereJpql = " and o.STATUS=1 and o.REGION_ID=" + tmpTVoteRegion.getId();
				}else if (status == 1){
					tmpWhereJpql = " and o.STATUS=0 and o.REGION_ID=" + tmpTVoteRegion.getId();
				}else{
					tmpWhereJpql = " and o.REGION_ID=" + tmpTVoteRegion.getId();
				}
			}else{
				if (status == 0){
					tmpWhereJpql = " and o.STATUS=1";
				}else if (status == 1){
					tmpWhereJpql = " and o.STATUS=0";
				}
			}
			tmpSql = tmpSql + tmpWhereJpql + orderBy;
			tmpListMap = entityDao.getScrollDataBySql(tmpSql, null, firstIndex, maxResult);
			if (tmpListMap == null || tmpListMap.isEmpty()){
				return resp;
			}
			
			List<GetVotePlayerListResp.VotePlayerInfo> tmpListVotePlayerInfo = new ArrayList<GetVotePlayerListResp.VotePlayerInfo>();
			GetVotePlayerListResp.VotePlayerInfo tmpVotePlayerInfo;
			String tmpStr;
			tmpSql = "select o.CODE as code,o.NAME as name from t_vote_region o where o.ID=%s";
			List<Map<String, Object>> tmpListRgMap;
			Map<String, Object> tmpRgMap;
			Map<Long, TVoteRegion> tmpVrMap = new HashMap<Long, TVoteRegion>();
			Long tmpRegionId;
			TVoteRegion tmpTVoteRegion2;
			for (Map<String, Object> tmpMap : tmpListMap){
				tmpVotePlayerInfo = new GetVotePlayerListResp.VotePlayerInfo();
				tmpStr = (String) tmpMap.get("avatar");
				if (!StringUtils.isBlank(tmpStr)){
					tmpVotePlayerInfo.setAvatar(getHttpUrl(tmpStr));
				}
				tmpStr = (String) tmpMap.get("avatarhd");
				if (!StringUtils.isBlank(tmpStr)){
					tmpVotePlayerInfo.setAvatarhd(getHttpUrl(tmpStr));
				}
				tmpVotePlayerInfo.setCode((String) tmpMap.get("code"));
				tmpVotePlayerInfo.setName((String) tmpMap.get("name"));
				tmpVotePlayerInfo.setProfile((String) tmpMap.get("profile"));
				tmpVotePlayerInfo.setRelCode((String) tmpMap.get("relCode"));
				tmpVotePlayerInfo.setRelCodehd((String) tmpMap.get("relCodehd"));
				tmpVotePlayerInfo.setVotes((Integer) tmpMap.get("votes"));
				tmpVotePlayerInfo.setVotesOther((Integer) tmpMap.get("votesOther"));
				tmpVotePlayerInfo.setVotesTotal((Integer) tmpMap.get("votesTotal"));
				
				if (tmpTVoteRegion != null){
					tmpVotePlayerInfo.setRegionCode(tmpTVoteRegion.getCode());
					tmpVotePlayerInfo.setRegionName(tmpTVoteRegion.getName());
				}else{
					tmpBig = (BigInteger) tmpMap.get("regionId");
					if (tmpBig != null){
						tmpRegionId = tmpBig.longValue();
						tmpTVoteRegion2 = tmpVrMap.get(tmpRegionId);
						if (tmpTVoteRegion2 == null){
							tmpListRgMap = entityDao.getScrollDataBySql(String.format(tmpSql, tmpRegionId), null, -1, -1);
							if (tmpListRgMap != null && !tmpListRgMap.isEmpty()){
								tmpRgMap = tmpListRgMap.get(0);
								tmpTVoteRegion2 = new TVoteRegion();
								tmpTVoteRegion2.setCode((String) tmpRgMap.get("code"));
								tmpTVoteRegion2.setName((String) tmpRgMap.get("name"));
								tmpVrMap.put(tmpRegionId, tmpTVoteRegion2);
							}
						}
						if (tmpTVoteRegion2 != null){
							tmpVotePlayerInfo.setRegionCode(tmpTVoteRegion2.getCode());
							tmpVotePlayerInfo.setRegionName(tmpTVoteRegion2.getName());
						}
						
					}
				}
				
				tmpVotePlayerInfo.setPlayerNo((String) tmpMap.get("playerNo"));
				tmpVotePlayerInfo.setStatus((Integer) tmpMap.get("status"));
				tmpVotePlayerInfo.setStartTime(DateFormatUtil.format((Date) tmpMap.get("startTime")));
				tmpVotePlayerInfo.setEndTime(DateFormatUtil.format((Date) tmpMap.get("endTime")));
				tmpListVotePlayerInfo.add(tmpVotePlayerInfo);
			}
			
			resp.setVotePlayerInfos(tmpListVotePlayerInfo);
			logger.info(resultDesc);
		}catch(Exception e){
			resultDesc = String.format("投票活动[%s]获取参赛选手失败，程序异常！", voteCode);
			logger.error(resultDesc, e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
	}
	
	public GetVotePlayerListOnlyResp getVotePlayerListOnlyByVoteId(long voteId, int firstIndex, int maxResult){
		GetVotePlayerListOnlyResp resp = new GetVotePlayerListOnlyResp();
		String resultDesc = String.format("投票活动[%s]获取参赛选手数量成功！", voteId);
		try{
			String tmpSql = getVotePlayerSelectSql() + " where o.VOTE_ID=" + voteId + " order by o.VOTES desc";
			Query query = entityDao.getEntityManager().createNativeQuery(tmpSql);
			query.setFirstResult(firstIndex).setMaxResults(maxResult);
			@SuppressWarnings("unchecked")
			List<Object[]> tmpListMap = query.getResultList();
			List<GetVotePlayerListResp.VotePlayerInfo> tmpListVotePlayerInfo = new ArrayList<GetVotePlayerListResp.VotePlayerInfo>();
			GetVotePlayerListResp.VotePlayerInfo tmpVotePlayerInfo;
			for (Object[] obj : tmpListMap){
				tmpVotePlayerInfo = new GetVotePlayerListResp.VotePlayerInfo();
				tmpVotePlayerInfo.setCode((String)obj[1]);
				tmpVotePlayerInfo.setName((String)obj[2]);
				tmpVotePlayerInfo.setPlayerNo((String)obj[3]);
				tmpVotePlayerInfo.setRelCode((String)obj[4]);
				tmpVotePlayerInfo.setRelCodehd((String)obj[5]);
				tmpVotePlayerInfo.setAvatar(getHttpUrl((String)obj[5]));
				tmpVotePlayerInfo.setAvatarhd(getHttpUrl((String)obj[7]));
				tmpVotePlayerInfo.setVotes((Integer)obj[8]);
				tmpVotePlayerInfo.setVotesOther((Integer)obj[9]);
				tmpVotePlayerInfo.setVotesTotal((Integer)obj[10]);
				//11
				tmpVotePlayerInfo.setProfile((String)obj[12]);
				//13（sequence）、14（createTime）、15（updateTime）、
				tmpVotePlayerInfo.setStatus((Integer)obj[16]);
				//17（cpCode）、18（regionId）、19（VOTE_TIMES）、20（voteTimesHd）
				//21（voteTimes4k）、22（verctrl）
				Timestamp timestamp = (Timestamp) obj[23];
				if (timestamp != null){
					tmpVotePlayerInfo.setStartTime(DateFormatUtil.format(new Date(timestamp.getTime()), "yyyy-MM-dd HH:mm:ss"));
				}
				timestamp = (Timestamp) obj[24];
				if (timestamp != null){
					tmpVotePlayerInfo.setEndTime(DateFormatUtil.format(new Date(timestamp.getTime()), "yyyy-MM-dd HH:mm:ss"));
				}
				tmpListVotePlayerInfo.add(tmpVotePlayerInfo);
			}
			
			resp.setVotePlayerInfos(tmpListVotePlayerInfo);
			logger.info(resultDesc);
		}catch(Exception e){
			resultDesc = String.format("投票活动[%s]获取参赛选手失败，程序异常！", voteId);
			logger.error(resultDesc, e);
		}
		return resp;
	}
	
	private int getQrVotes(String voteCode, Integer qrcodeSwitch, String userID) throws SQLException{
		int tmpQrVotes = 0;
		if (qrcodeSwitch == null || qrcodeSwitch == 0){
			return tmpQrVotes;
		}
		List<TVoteQrcodeDetail> tmpListTVoteQrcodeDetail = entityDao.getScrollData(TVoteQrcodeDetail.class,
				"o.tvoteQrcode.tvoteGame.code=?1 and o.status=1 and o.userId=?2 and date_format(o.bindTime,'%Y-%m-%d')=?3", 
				new Object[]{voteCode, userID, DateFormatUtil.format(new Date(), "yyyy-MM-dd")});
		if(tmpListTVoteQrcodeDetail != null && !tmpListTVoteQrcodeDetail.isEmpty()){
			int tmpTVoteQrcodeStatus;
			int tmpTVoteGameStatus;
			int tmpVotes;
			TVoteQrcode tmpTVoteQrcode;
			for (TVoteQrcodeDetail tmpTVoteQrcodeDetail : tmpListTVoteQrcodeDetail){
				tmpTVoteQrcode = tmpTVoteQrcodeDetail.getTvoteQrcode();
				tmpTVoteQrcodeStatus = tmpTVoteQrcode.getStatus();
				tmpTVoteGameStatus = tmpTVoteQrcode.getTvoteGame().getStatus();
				if (tmpTVoteQrcodeStatus == 0 || tmpTVoteGameStatus == 0){
					continue;
				}
				tmpVotes = tmpTVoteQrcode.getVotes() == null ? 0 : tmpTVoteQrcode.getVotes();
				tmpQrVotes = tmpQrVotes + tmpVotes;
			}
		}
		return tmpQrVotes;
	}
	
	/**
	 * 获取用户当天已投票数
	 * @param voteId
	 * @param userID
	 * @return
	 * @throws SQLException
	 */
	private int getDayVotes(Long voteId, String userID) throws SQLException{
		int tmpDayVotes = 0;
		String tmpSql = "select sum(o.VOTES) as count from t_vote_record_day o where o.VOTE_ID=? and o.USER_ID=? " +
				"and date_format(o.CREATE_TIME,'%Y-%m-%d')=?";
		List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, new Object[]{voteId, userID, 
				DateFormatUtil.format(new Date(), "yyyy-MM-dd")}, -1, -1);
		
		if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
			Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
			BigDecimal tmpBigVoteCount = (BigDecimal) tmpMapCount.get("count");
			tmpDayVotes = tmpBigVoteCount == null ? 0 : tmpBigVoteCount.intValue();
		}
		return tmpDayVotes;
	}
	
	/**
	 * 获取用户活动期间已投票数
	 * @param tvoteGame
	 * @param userID
	 * @return
	 * @throws SQLException
	 */
	private int getPeriodVotes(TVoteGame tvoteGame, String userID) throws SQLException{
		int tmpPeriodVotes = 0;
		String tmpSql = "select sum(o.VOTES) as count from t_vote_record o where o.VOTE_ID=? and o.USER_ID=? " +
				"and date_format(o.CREATE_TIME,'%Y-%m-%d %H:%i:%s')>=? and date_format(o.CREATE_TIME,'%Y-%m-%d %H:%i:%s')<=?";
		List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, new Object[]{tvoteGame.getId(), userID, 
				DateFormatUtil.format(tvoteGame.getStartTime(), "yyyy-MM-dd HH:mm:ss"), 
				DateFormatUtil.format(tvoteGame.getEndTime(), "yyyy-MM-dd HH:mm:ss")}, -1, -1);
		
		if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
			Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
			BigDecimal tmpBigVoteCount = (BigDecimal) tmpMapCount.get("count");
			tmpPeriodVotes = tmpBigVoteCount == null ? 0 : tmpBigVoteCount.intValue();
			
		}
		return tmpPeriodVotes;
	}
	
	/**
	 * 获取区域下所有选手投票数总和
	 * @param tvoteGame
	 * @param userID
	 * @return
	 * @throws SQLException
	 */
	private int getRegionPlayerVotes(Long voteId, Long regionId) throws SQLException{
		int tmpRegionPlayerVotes = 0;
		String tmpSql = "select sum(o.VOTES) as count from t_vote_player o where o.VOTE_ID=? and o.REGION_ID=?";
		List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, new Object[]{voteId, regionId}, -1, -1);
		
		if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
			Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
			BigDecimal tmpBigVoteCount = (BigDecimal) tmpMapCount.get("count");
			tmpRegionPlayerVotes = tmpBigVoteCount == null ? 0 : tmpBigVoteCount.intValue();
			
		}
		return tmpRegionPlayerVotes;
	}
	
	/**
	 * 获取用户当天该区域投票次数
	 * @param tvoteGame
	 * @param tvoteRegion
	 * @param userID
	 * @return
	 * @throws SQLException
	 */
	private int getDayRegionVoteTimes(TVoteGame tvoteGame, TVoteRegion tvoteRegion, String userID) throws SQLException{
		int tmpDayRegionVoteTimes = 0;
		if (tvoteRegion == null) return tmpDayRegionVoteTimes;
		//1.查询区域下所有选手
		List<TVotePlayer> tmpListTVotePlayer = entityDao.getScrollData(TVotePlayer.class,
				"o.tvoteGame.id=?1 and o.tvoteRegion.id=?2 and o.status=1", 
				new Object[]{tvoteGame.getId(), tvoteRegion.getId()});
		if (tmpListTVotePlayer == null || tmpListTVotePlayer.size() == 0) return tmpDayRegionVoteTimes;
		
		List<Object> tmpListQueryParam = new ArrayList<Object>();
		tmpListQueryParam.add(tvoteGame.getId());
		tmpListQueryParam.add(userID);
		tmpListQueryParam.add(DateFormatUtil.format(new Date(), "yyyy-MM-dd"));
		StringBuilder tmpInSb = new StringBuilder("");
		TVotePlayer tmpTVotePlayer;
		for (int i = 0; i < tmpListTVotePlayer.size(); i++){
			tmpTVotePlayer = tmpListTVotePlayer.get(i);
			if (tmpTVotePlayer == null || StringUtils.isBlank(tmpTVotePlayer.getCode())) continue;
			tmpInSb.append("?").append(",");
			tmpListQueryParam.add(tmpTVotePlayer.getCode());
		}
		if (tmpInSb.length() > 0) tmpInSb.deleteCharAt(tmpInSb.length() -1);
		
		String tmpSql = "select count(o.id) as count from t_vote_record_day o where o.VOTE_ID=? and o.USER_ID=? " +
				"and date_format(o.CREATE_TIME,'%Y-%m-%d')=? and o.PLAYER_CODE in (" + tmpInSb.toString() + ")";
		List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, tmpListQueryParam.toArray(), -1, -1);
		
		if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
			Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
			BigInteger tmpBigVoteTimes = (BigInteger) tmpMapCount.get("count");
			tmpDayRegionVoteTimes = tmpBigVoteTimes == null ? 0 : tmpBigVoteTimes.intValue();
			
		}
		return tmpDayRegionVoteTimes;
	}
	
	/**
	 * 获取用户活动期间该区域投票次数
	 * @param tvoteGame
	 * @param tvoteRegion
	 * @param userID
	 * @return
	 * @throws SQLException
	 */
	private int getPeriodRegionVoteTimes(TVoteGame tvoteGame, TVoteRegion tvoteRegion, String userID) throws SQLException{
		int tmpPeriodRegionVoteTimes = 0;
		if (tvoteRegion == null) return tmpPeriodRegionVoteTimes;
		//1.查询区域下所有选手
		List<TVotePlayer> tmpListTVotePlayer = entityDao.getScrollData(TVotePlayer.class, 
				"o.tvoteGame.id=?1 and o.tvoteRegion.id=?2 and o.status=1", 
				new Object[]{tvoteGame.getId(), tvoteRegion.getId()});
		if (tmpListTVotePlayer == null || tmpListTVotePlayer.size() == 0) return tmpPeriodRegionVoteTimes;
		
		List<Object> tmpListQueryParam = new ArrayList<Object>();
		tmpListQueryParam.add(tvoteGame.getId());
		tmpListQueryParam.add(userID);
		tmpListQueryParam.add(DateFormatUtil.format(tvoteGame.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
		tmpListQueryParam.add(DateFormatUtil.format(tvoteGame.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
		StringBuilder tmpInSb = new StringBuilder("");
		TVotePlayer tmpTVotePlayer;
		for (int i = 0; i < tmpListTVotePlayer.size(); i++){
			tmpTVotePlayer = tmpListTVotePlayer.get(i);
			if (tmpTVotePlayer == null || StringUtils.isBlank(tmpTVotePlayer.getCode())) continue;
			tmpInSb.append("?").append(",");
			tmpListQueryParam.add(tmpTVotePlayer.getCode());
		}
		if (tmpInSb.length() > 0) tmpInSb.deleteCharAt(tmpInSb.length() -1);
		
		String tmpSql = "select count(o.id) as count from t_vote_record o where o.VOTE_ID=? and o.USER_ID=? " +
				"and date_format(o.CREATE_TIME,'%Y-%m-%d %H:%i:%s')>=? " +
				"and date_format(o.CREATE_TIME,'%Y-%m-%d %H:%i:%s')<=? " +
				"and o.PLAYER_CODE in (" + tmpInSb.toString() + ")";
		List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, tmpListQueryParam.toArray(), -1, -1);
		
		if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
			Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
			BigInteger tmpBigVoteTimes = (BigInteger) tmpMapCount.get("count");
			tmpPeriodRegionVoteTimes = tmpBigVoteTimes == null ? 0 : tmpBigVoteTimes.intValue();
			
		}
		return tmpPeriodRegionVoteTimes;
	}
	
	/**
	 * 获取用户当天该选手投票次数
	 * @param tvoteGame
	 * @param tvotePlayer
	 * @param userID
	 * @return
	 * @throws SQLException
	 */
	private int getDayPlayerVoteTimes(TVoteGame tvoteGame, TVotePlayer tvotePlayer, String userID) throws SQLException{
		int tmpDayPlayerVoteTimes = 0;
		if (tvotePlayer == null) return tmpDayPlayerVoteTimes;
		
		String tmpSql = "select count(o.id) as count from t_vote_record_day o where o.VOTE_ID=? and o.USER_ID=? " +
				"and date_format(o.CREATE_TIME,'%Y-%m-%d')=? and o.PLAYER_CODE =?";
		List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, new Object[]{
				tvoteGame.getId(), 
				userID,
				DateFormatUtil.format(new Date(), "yyyy-MM-dd"),
				tvotePlayer.getCode()}, -1, -1);
		
		if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
			Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
			BigInteger tmpBigVoteTimes = (BigInteger) tmpMapCount.get("count");
			tmpDayPlayerVoteTimes = tmpBigVoteTimes == null ? 0 : tmpBigVoteTimes.intValue();
			
		}
		return tmpDayPlayerVoteTimes;
	}
	
	/**
	 * 获取用户活动期间该选手投票次数
	 * @param tvoteGame
	 * @param tvotePlayer
	 * @param userID
	 * @return
	 * @throws SQLException
	 */
	private int getPeriodPlayerVoteTimes(TVoteGame tvoteGame, TVotePlayer tvotePlayer, String userID) throws SQLException{
		int tmpPlayerRegionVoteTimes = 0;
		if (tvotePlayer == null) return tmpPlayerRegionVoteTimes;
		
		String tmpSql = "select count(o.id) as count from t_vote_record o where o.VOTE_ID=? and o.USER_ID=? " +
				"and date_format(o.CREATE_TIME,'%Y-%m-%d %H:%i:%s')>=? " +
				"and date_format(o.CREATE_TIME,'%Y-%m-%d %H:%i:%s')<=? " +
				"and o.PLAYER_CODE = ?";
		List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, 
				new Object[]{tvoteGame.getId(),
				userID,
				DateFormatUtil.format(tvoteGame.getStartTime(), "yyyy-MM-dd HH:mm:ss"),
				DateFormatUtil.format(tvoteGame.getEndTime(), "yyyy-MM-dd HH:mm:ss"), 
				tvotePlayer.getCode()}, -1, -1);
		
		if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
			Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
			BigInteger tmpBigVoteTimes = (BigInteger) tmpMapCount.get("count");
			tmpPlayerRegionVoteTimes = tmpBigVoteTimes == null ? 0 : tmpBigVoteTimes.intValue();
			
		}
		return tmpPlayerRegionVoteTimes;
	}
	
	/**
	 * 限制用户在有效期内只能向某一区域的参赛选手投票次数:0-不做限制
	 * @param tvoteRegion
	 * @param epgType
	 * @return
	 */
	private int getRegionVoteTimesLimit(TVoteRegion tvoteRegion, int epgType){
		int tmpVoteTimesLimit = 0;
		if (tvoteRegion == null) return tmpVoteTimesLimit;
		if (epgType == EpgType.HIGH.getType()){
			tmpVoteTimesLimit = tvoteRegion.getVoteTimesHd() == null ? 0 : tvoteRegion.getVoteTimesHd();
		}else if (epgType == EpgType.FOURK.getType()){
			tmpVoteTimesLimit = tvoteRegion.getVoteTimes4k() == null ? 0 : tvoteRegion.getVoteTimes4k();
		}else{
			epgType = EpgType.STANDARD.getType();
			tmpVoteTimesLimit = tvoteRegion.getVoteTimes() == null ? 0 : tvoteRegion.getVoteTimes();
		}
		if (tmpVoteTimesLimit < 0) tmpVoteTimesLimit = 0;
		return tmpVoteTimesLimit;
	}
	
	/**
	 * 限制用户在有效期内只能向某一参赛选手投票次数:0-不做限制
	 * @param tvotePlayer
	 * @param epgType
	 * @return
	 */
	private int getPlayerVoteTimesLimit(TVotePlayer tvotePlayer, int epgType){
		int tmpVoteTimesLimit = 0;
		if (tvotePlayer == null) return tmpVoteTimesLimit;
		if (epgType == EpgType.HIGH.getType()){
			tmpVoteTimesLimit = tvotePlayer.getVoteTimesHd() == null ? 0 : tvotePlayer.getVoteTimesHd();
		}else if (epgType == EpgType.FOURK.getType()){
			tmpVoteTimesLimit = tvotePlayer.getVoteTimes4k() == null ? 0 : tvotePlayer.getVoteTimes4k();
		}else{
			epgType = EpgType.STANDARD.getType();
			tmpVoteTimesLimit = tvotePlayer.getVoteTimes() == null ? 0 : tvotePlayer.getVoteTimes();
		}
		if (tmpVoteTimesLimit < 0) tmpVoteTimesLimit = 0;
		return tmpVoteTimesLimit;
	}
	
	private int getVoteTimes(TVoteGame tvoteGame, int epgType){
		int tmpVoteTimes;
		if (epgType == EpgType.HIGH.getType()){
			tmpVoteTimes = tvoteGame.getVoteTimesHd() == null ? 0 : tvoteGame.getVoteTimesHd();
		}else if (epgType == EpgType.FOURK.getType()){
			tmpVoteTimes = tvoteGame.getVoteTimes4k() == null ? 0 : tvoteGame.getVoteTimes4k();
		}else{
			epgType = EpgType.STANDARD.getType();
			tmpVoteTimes = tvoteGame.getVoteTimes() == null ? 0 : tvoteGame.getVoteTimes();
		}
		return tmpVoteTimes;
	}
	
	@Transactional
	public LuckyVoteResp addUserVote(String voteCode, String userID, String productID, int epgType) {
		LuckyVoteResp resp = new LuckyVoteResp();
		resp.setCanVoteCount(0);
		resp.setRemainVoteCount(0);
		Date currentDate = new Date();
		String resultDesc = String.format("初始化用户[%s]票数成功！", userID);
		try{
			if (StringUtils.isBlank(voteCode)){
				resultDesc = String.format("初始化用户[%s]票数失败，投票活动编码非法！", userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			if (StringUtils.isBlank(userID)){
				resultDesc = String.format("初始化用户[%s]票数失败，用户账号非法！", userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			if (epgType > EpgType.FOURK.getType()){
				resultDesc = String.format("初始化用户[%s]票数失败，epgType[%s]非法！", userID, epgType);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("初始化用户[%s]票数失败，投票活动不存在！", userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.VOTE_DOES_NOT_EXIST);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			//验证投票活动有效性
			ValidateRets<TVoteGame>  tmpTVoteGameValidateRets = voteGameValidate.validate(new VoteGameValidateArgs(resp, 
					tmpTVoteGame, currentDate));
			if (!tmpTVoteGameValidateRets.isSuccess()) return resp;
			
			TVoteUser tmpTVoteUser = getTVoteUser(tmpTVoteGame, userID);
			
			//2016-07-26 添加基础投票权利
			int tmpVoteBase = tmpTVoteGame.getVoteBase() == null ? 1 : tmpTVoteGame.getVoteBase();
			int tmpBeVotes = getVoteTimes(tmpTVoteGame, epgType) * tmpVoteBase;
			//2016-07-25 添加扫二维码送票活动
			int tmpQrVotes = getQrVotes(voteCode, tmpTVoteGame.getQrcodeSwitch(), userID);
			//用户已投票数
			int tmpHaveVotes;
			//2017-05-03增加用户投票次数有效期范围限制：活动每天或活动期间
			int tmpVoteTimesValidity = tmpTVoteGame.getVoteTimesValidity() == null ? VoteTimesValidity.DAY.getValidity() : tmpTVoteGame.getVoteTimesValidity();
			if (tmpVoteTimesValidity == VoteTimesValidity.DAY.getValidity()){
				//2016-08-08 获取用户当天已投票数
				tmpHaveVotes = getDayVotes(tmpTVoteGame.getId(), userID);
			}else{
				//2017-05-03获取活动期间用户已投票数
				tmpHaveVotes = getPeriodVotes(tmpTVoteGame, userID);
			}
			int tmpCanVoteCount;
			int tmpCanVoteTimes;
			int tmpCanVoteRemain;
			int tmpRemainVoteCount;
			int tmpRemainVoteTimes;
			int tmpRemainVoteRemain;
			
			String tmpProductVotes = "";
			String tmpProductCodes = "";
			int tmpTotalVote = 0;
			if (!StringUtils.isBlank(productID)){
				String[] tmpArrayProductID = productID.split(",");
				String tmpProductID;
				Map<String, Integer> tmpDestUserVoteMap = new LinkedHashMap<String, Integer>();
				
				String tmpSql = "select o.VOTES as votes from t_vote_product o where o.VOTE_ID=" + tmpTVoteGame.getId();
				Map<String, Object> tmpMap;
				Integer tmpVotes;
				for (int i=0;i<tmpArrayProductID.length; i++){
					tmpProductID = tmpArrayProductID[i];
					List<Map<String, Object>> tmpListMap;
					if (epgType == EpgType.NONE.getType()){
						tmpListMap = entityDao.getScrollDataBySql(tmpSql + " and STATUS=1 and o.PRODUCT_CODE=?", new Object[]{tmpProductID}, -1, -1);
					}else{
						tmpListMap = entityDao.getScrollDataBySql(tmpSql + " and STATUS=1 and o.PRODUCT_CODE=? and o.EPG_TYPE=" + epgType, new Object[]{tmpProductID}, -1, -1);
					}
					if (tmpListMap == null || tmpListMap.isEmpty()){
						continue;
					}
					tmpMap = tmpListMap.get(0);
					tmpVotes = (Integer) tmpMap.get("votes");
					
					if (tmpVotes == null || tmpVotes < 0) {
						continue;
					}
					
					tmpDestUserVoteMap.put(tmpProductID, tmpVotes);
					tmpTotalVote = tmpTotalVote + tmpVotes;
					tmpProductCodes = tmpProductCodes + tmpProductID + ",";
					tmpProductVotes = tmpProductVotes + tmpVotes + ",";
				}
			}
			
			if (StringUtils.isBlank(tmpProductCodes)){
				if (tmpTVoteUser != null){
					entityDao.deleteDetached(tmpTVoteUser);
				}
				tmpCanVoteCount = tmpBeVotes + tmpQrVotes;
				tmpCanVoteTimes = tmpCanVoteCount/tmpVoteBase;
				tmpCanVoteRemain = tmpCanVoteCount%tmpVoteBase;
				if (tmpCanVoteRemain > 0){
					tmpCanVoteTimes = tmpCanVoteTimes + 1;
				}
				tmpRemainVoteCount = tmpCanVoteCount - tmpHaveVotes;
				if (tmpRemainVoteCount < 0){
					tmpRemainVoteCount = 0;
				}
				tmpRemainVoteTimes = tmpRemainVoteCount/tmpVoteBase;
				tmpRemainVoteRemain = tmpRemainVoteCount%tmpVoteBase;
				if (tmpRemainVoteRemain > 0){
					tmpRemainVoteTimes = tmpRemainVoteTimes + 1;
				}
				resp.setCanVoteCount(tmpCanVoteCount);
				resp.setCanVoteTimes(tmpCanVoteTimes);
				resp.setRemainVoteCount(tmpRemainVoteCount);
				resp.setRemainVoteTimes(tmpRemainVoteTimes);
				logger.info(resultDesc);
				return resp;
			}
			tmpProductCodes = StringUtils.removeEnd(tmpProductCodes, ",");
			tmpProductVotes = StringUtils.removeEnd(tmpProductVotes, ",");
			tmpVoteBase = tmpTVoteGame.getVipVoteBase() == null ? 1 : tmpTVoteGame.getVipVoteBase();
			tmpCanVoteCount = tmpTotalVote + tmpBeVotes + tmpQrVotes;
			tmpCanVoteTimes = tmpCanVoteCount/tmpVoteBase;
			tmpCanVoteRemain = tmpCanVoteCount%tmpVoteBase;
			if (tmpCanVoteRemain > 0){
				tmpCanVoteTimes = tmpCanVoteTimes + 1;
			}
			tmpRemainVoteCount = tmpCanVoteCount - tmpHaveVotes;
			if (tmpRemainVoteCount < 0){
				tmpRemainVoteCount = 0;
			}
			tmpRemainVoteTimes = tmpRemainVoteCount/tmpVoteBase;
			tmpRemainVoteRemain = tmpRemainVoteCount%tmpVoteBase;
			if (tmpRemainVoteRemain > 0){
				tmpRemainVoteTimes = tmpRemainVoteTimes + 1;
			}
			if (tmpTVoteUser == null){
				tmpTVoteUser = new TVoteUser();
				tmpTVoteUser.setVoteBase(tmpBeVotes);
				tmpTVoteUser.setVoteQrcode(tmpQrVotes);
				tmpTVoteUser.setCanVotes(tmpCanVoteCount);
				tmpTVoteUser.setCpCode(tmpTVoteGame.getCpCode());
				tmpTVoteUser.setCreateTime(currentDate);
				tmpTVoteUser.setProductCodes(tmpProductCodes);
				tmpTVoteUser.setProductVotes(tmpProductVotes);
				tmpTVoteUser.setRemainVotes(tmpRemainVoteCount);
				tmpTVoteUser.setStatus(1);
				tmpTVoteUser.setTvoteGame(tmpTVoteGame);
				tmpTVoteUser.setUserId(userID);
				tmpTVoteUser.setEpgType(epgType);
				entityDao.saveObj(tmpTVoteUser);
			}else{
				tmpTVoteUser.setVoteBase(tmpBeVotes);
				tmpTVoteUser.setVoteQrcode(tmpQrVotes);
				tmpTVoteUser.setCanVotes(tmpCanVoteCount);
				tmpTVoteUser.setRemainVotes(tmpRemainVoteCount);
				tmpTVoteUser.setProductCodes(tmpProductCodes);
				tmpTVoteUser.setProductVotes(tmpProductVotes);
				tmpTVoteUser.setUpdateTime(currentDate);
				tmpTVoteUser.setEpgType(epgType);
				entityDao.update(tmpTVoteUser);
			}
			logger.info(resultDesc);
			resp.setCanVoteCount(tmpCanVoteCount);
			resp.setRemainVoteCount(tmpRemainVoteCount);
			resp.setCanVoteTimes(tmpCanVoteTimes);
			resp.setRemainVoteTimes(tmpRemainVoteTimes);
		}catch(Exception e){
			resultDesc = String.format("初始化用户[%s]票数失败，程序异常！", userID);
			logger.error(resultDesc, e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
	}
	
	@Transactional
	public LuckyVoteResp reduceUserVote(String voteCode, String userID, String productID, int epgType) {
		LuckyVoteResp resp = new LuckyVoteResp();
		resp.setCanVoteCount(0);
		resp.setRemainVoteCount(0);
		Date currentDate = new Date();
		String resultDesc = String.format("减少用户[%s]票数成功！", userID);
		try{
			if (StringUtils.isBlank(voteCode)){
				resultDesc = String.format("减少用户[%s]票数失败，投票活动编码非法！", userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			if (StringUtils.isBlank(userID)){
				resultDesc = String.format("减少用户[%s]票数失败，用户账号非法！", userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			if (StringUtils.isBlank(productID)){
				resultDesc = String.format("减少用户[%s]票数失败，产品编码非法！", userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			if (epgType > EpgType.FOURK.getType()){
				resultDesc = String.format("减少用户[%s]票数失败，epgType[%s]非法！", userID, epgType);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			if (epgType == EpgType.NONE.getType()){
				epgType = EpgType.STANDARD.getType();
			}
			List<String> tmpDestList = new ArrayList<String>();
			String[] tmpDestArrayProductID = productID.split(",");
			for (int i=0;i<tmpDestArrayProductID.length; i++){
				for (int j=0;j<tmpDestArrayProductID.length && j!=i ;j++){
					if (tmpDestArrayProductID[i].equals(tmpDestArrayProductID[j])){
						resultDesc = String.format("减少用户[%s]票数失败，产品编码组合不允许重复！", userID);
						logger.error(resultDesc);
						resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
						resp.setResultDesc(resultDesc);
						return resp;
					}
				}
				tmpDestList.add(tmpDestArrayProductID[i]);
			}
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("减少用户[%s]票数失败，投票活动不存在！", userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.VOTE_DOES_NOT_EXIST);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			//验证投票活动有效性
			ValidateRets<TVoteGame>  tmpTVoteGameValidateRets = voteGameValidate.validate(new VoteGameValidateArgs(resp, 
					tmpTVoteGame, currentDate));
			if (!tmpTVoteGameValidateRets.isSuccess()) return resp;
			
			TVoteUser tmpTVoteUser = entityDao.get(TVoteUser.class, "o.userId=?1 and o.tvoteGame.code=?2", 
					new Object[]{userID, voteCode});
			if (tmpTVoteUser == null){
				resultDesc = String.format("减少用户[%s]票数失败，投票用户记录不存在！", userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			String productCodes = tmpTVoteUser.getProductCodes();
			String productVotes = tmpTVoteUser.getProductVotes();
			if (StringUtils.isBlank(productCodes) && StringUtils.isBlank(productVotes)){
				resultDesc = String.format("减少用户[%s]票数失败，用户未订购指定产品！", userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			if (!(!StringUtils.isBlank(productCodes) && !StringUtils.isBlank(productVotes))){
				resultDesc = String.format("减少用户[%s]票数失败，数据库数据错误，产品编码与产品票数不匹配！", userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.DATABASE_EXCEPTION);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			String[] tmpSrcArrayProductCodes = productCodes.split(",");
			String[] tmpSrcArrayProductVotes = productVotes.split(",");
			if (tmpSrcArrayProductCodes.length != tmpSrcArrayProductVotes.length){
				resultDesc = String.format("减少用户[%s]票数失败，数据库数据错误，产品编码与产品票数不匹配！", userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.DATABASE_EXCEPTION);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			//TODO 不作数据产品编码与产品投票数据有效性检查，默认是正确的（比如产品编码重复检查、产品投票负数检查）
			Map<String, Integer> tmpSrcMap = new LinkedHashMap<String, Integer>();
			for (int i=0; i<tmpSrcArrayProductCodes.length;i++){
				tmpSrcMap.put(tmpSrcArrayProductCodes[i], NumberUtils.toInt(tmpSrcArrayProductVotes[i]));
			}
			String tmpProductCodes = "";
			String tmpProductVotes = "";
			int tmpTotalDiffer = 0;
			//Set<String> tmpSrcSet = tmpSrcMap.keySet();
			for (Entry<String, Integer> entry : tmpSrcMap.entrySet()){
				if (!tmpDestList.contains(entry.getKey())){
					tmpProductCodes = tmpProductCodes + entry.getKey() + ",";
					tmpProductVotes = tmpProductVotes + entry.getValue() + ",";
				}else{
					tmpTotalDiffer = tmpTotalDiffer + entry.getValue();
				}
			}
			
			/*for (String tmpDest : tmpDestList){
				if (!tmpSrcSet.contains(tmpDest)){
					resultDesc = String.format("减少用户[%s]票数失败，用户未订购产品[%s]！", userID, tmpDest);
					logger.error(resultDesc);
					resp.setResultCode(LuckyVoteResultCode.DATABASE_EXCEPTION);
					resp.setResultDesc(resultDesc);
					return resp;
				}
			}*/
			
			/**
			 * 2016-07-26 添加基础投票权利
			 * */
			int tmpVoteBase = tmpTVoteGame.getVoteBase() == null ? 1 : tmpTVoteGame.getVoteBase();
			int tmpVoteTimes;
			if (epgType == EpgType.HIGH.getType()){
				tmpVoteTimes = tmpTVoteGame.getVoteTimesHd() == null ? 0 : tmpTVoteGame.getVoteTimesHd();
			}else if (epgType == EpgType.FOURK.getType()){
				tmpVoteTimes = tmpTVoteGame.getVoteTimes4k() == null ? 0 : tmpTVoteGame.getVoteTimes4k();
			}else{
				tmpVoteTimes = tmpTVoteGame.getVoteTimes() == null ? 0 : tmpTVoteGame.getVoteTimes();
			}
			int tmpBeVotes = tmpVoteTimes * tmpVoteBase;
			/**
			 * 2016-07-26 添加扫二维码送票活动
			 * */
			int tmpQrVotes = getQrVotes(voteCode, tmpTVoteGame.getQrcodeSwitch(), userID);
			
			tmpProductCodes = StringUtils.removeEnd(tmpProductCodes, ",");
			tmpProductVotes = StringUtils.removeEnd(tmpProductVotes, ",");
			int tmpIntTotalVote = tmpTVoteUser.getCanVotes() - tmpTotalDiffer;
			int tmpRemainTotalVote = tmpTVoteUser.getRemainVotes() - tmpTotalDiffer;
			
			int tmpSrcVoteBase = tmpTVoteUser.getVoteBase() == null ? 0 : tmpTVoteUser.getVoteBase();
			int tmpSrcVoteQrcode = tmpTVoteUser.getVoteQrcode() == null ? 0 : tmpTVoteUser.getVoteQrcode();
			int tmpTotalDiv = tmpBeVotes + tmpQrVotes - tmpSrcVoteBase - tmpSrcVoteQrcode;
			tmpIntTotalVote = tmpIntTotalVote + tmpTotalDiv;
			tmpRemainTotalVote = tmpRemainTotalVote + tmpTotalDiv;
			
			if (tmpIntTotalVote < 0) tmpIntTotalVote = 0;
			if (tmpRemainTotalVote < 0) tmpRemainTotalVote = 0;
			//退订了，删除记录
			if (tmpIntTotalVote == 0){
				entityDao.deleteDetached(tmpTVoteUser);
				logger.info(resultDesc + "，删除投票用户成功！");
			}else{
				tmpTVoteUser.setCanVotes(tmpIntTotalVote);
				tmpTVoteUser.setRemainVotes(tmpRemainTotalVote);
				tmpTVoteUser.setProductCodes(tmpProductCodes);
				tmpTVoteUser.setProductVotes(tmpProductVotes);
				tmpTVoteUser.setUpdateTime(currentDate);
				tmpTVoteUser.setEpgType(epgType);
				entityDao.update(tmpTVoteUser);
				logger.info(resultDesc + "，更新用户票数成功！");
			}
			resp.setCanVoteCount(tmpIntTotalVote);
			resp.setRemainVoteCount(tmpRemainTotalVote);
			int tmpVipVoteBase = tmpTVoteGame.getVipVoteBase() == null ? 1 : tmpTVoteGame.getVipVoteBase();
			int tmpCanVoteTimes = tmpIntTotalVote/tmpVipVoteBase;
			int tmpCanVoteRemain = tmpIntTotalVote%tmpVipVoteBase;
			if (tmpCanVoteRemain > 0){
				tmpCanVoteTimes = tmpCanVoteTimes + 1;
			}
			int tmpRemainVoteTimes = tmpRemainTotalVote/tmpVipVoteBase;
			int tmpRemainVoteRemain = tmpRemainTotalVote%tmpVipVoteBase;
			if (tmpRemainVoteRemain > 0){
				tmpRemainVoteTimes = tmpRemainVoteTimes + 1;
			}
			resp.setCanVoteTimes(tmpCanVoteTimes);
			resp.setRemainVoteTimes(tmpRemainVoteTimes);
		}catch(Exception e){
			resultDesc = String.format("减少用户[%s]票数失败，程序异常！", userID);
			logger.error(resultDesc, e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
		
	}

	public LuckyVoteResp getVoteCount(String voteCode, String userID, int epgType) {
		LuckyVoteResp resp = new LuckyVoteResp();
		resp.setCanVoteCount(0);
		resp.setRemainVoteCount(0);
		String resultDesc = String.format("获取用户[%s]投票计数成功！", userID);
		try{
			if (StringUtils.isBlank(userID)){
				resultDesc = String.format("获取用户[%s]投票计数失败，用户账号[%s]非法！", userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			if (StringUtils.isBlank(voteCode)){
				resultDesc = String.format("获取用户[%s]投票计数失败，投票活动编码[%s]非法！", userID, voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("获取用户[%s]投票计数失败，投票活动编码[%s]非法！", userID, voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			
			TVoteUser tmpTVoteUser = getTVoteUser(tmpTVoteGame, userID);
			
			//2016-07-26 添加基础投票权利
			int tmpVoteBase = tmpTVoteGame.getVoteBase() == null ? 1 : tmpTVoteGame.getVoteBase();
			int tmpBeVotes = getVoteTimes(tmpTVoteGame, epgType) * tmpVoteBase;
			//2016-07-25 添加扫二维码送票活动
			int tmpQrVotes = getQrVotes(voteCode, tmpTVoteGame.getQrcodeSwitch(), userID);
			
			//用户已投票数
			int tmpHaveVotes;
			//2017-05-03增加用户投票次数有效期范围限制：活动每天或活动期间
			int tmpVoteTimesValidity = tmpTVoteGame.getVoteTimesValidity() == null ? VoteTimesValidity.DAY.getValidity() : tmpTVoteGame.getVoteTimesValidity();
			if (tmpVoteTimesValidity == VoteTimesValidity.DAY.getValidity()){
				//2016-08-08 获取用户当天已投票数
				tmpHaveVotes = getDayVotes(tmpTVoteGame.getId(), userID);
			}else{
				//2017-05-03获取活动期间用户已投票数
				tmpHaveVotes = getPeriodVotes(tmpTVoteGame, userID);
			}
			
			int tmpCanVoteCount;
			int tmpCanVoteTimes;
			int tmpCanVoteRemain;
			int tmpRemainVoteCount;
			int tmpRemainVoteTimes;
			int tmpRemainVoteRemain;
			if (tmpTVoteUser == null){
				tmpCanVoteCount = tmpBeVotes + tmpQrVotes;
				tmpCanVoteTimes = tmpCanVoteCount/tmpVoteBase;
				tmpCanVoteRemain = tmpCanVoteCount%tmpVoteBase;
				if (tmpCanVoteRemain > 0){
					tmpCanVoteTimes = tmpCanVoteTimes + 1;
				}
				tmpRemainVoteCount = tmpCanVoteCount - tmpHaveVotes;
				if (tmpRemainVoteCount < 0){
					tmpRemainVoteCount = 0;
				}
				tmpRemainVoteTimes = tmpRemainVoteCount/tmpVoteBase;
				tmpRemainVoteRemain = tmpRemainVoteCount%tmpVoteBase;
				if (tmpRemainVoteRemain > 0){
					tmpRemainVoteTimes = tmpRemainVoteTimes + 1;
				}
				resp.setCanVoteCount(tmpCanVoteCount);
				resp.setCanVoteTimes(tmpCanVoteTimes);
				resp.setRemainVoteCount(tmpRemainVoteCount);
				resp.setRemainVoteTimes(tmpRemainVoteTimes);
				logger.info(resultDesc);
				return resp;
			}
			if (tmpTVoteUser.getStatus() == 0){
				resultDesc = String.format("[%s]投票失败，用户被禁用投票！",userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.USER_VOTE_BEEN_DISABLED);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			int tmpVipVoteBase = tmpTVoteGame.getVipVoteBase() == null ? 1 : tmpTVoteGame.getVipVoteBase();
			tmpCanVoteCount = tmpTVoteUser.getCanVotes() == null ? 0 : tmpTVoteUser.getCanVotes();
			int tmpSrcQrVotes = tmpTVoteUser.getVoteQrcode() == null ? 0 : tmpTVoteUser.getVoteQrcode();
			int tmpSrcBaseVotes = tmpTVoteUser.getVoteBase() == null ? 0 : tmpTVoteUser.getVoteBase();
			int tmpDivQrVotes = tmpQrVotes - tmpSrcQrVotes;
			int tmpDivBaseVotes = tmpBeVotes - tmpSrcBaseVotes;
			tmpCanVoteCount = tmpCanVoteCount + tmpDivQrVotes + tmpDivBaseVotes;
			if (tmpCanVoteCount < 0) tmpCanVoteCount = 0;
			tmpCanVoteTimes = tmpCanVoteCount/tmpVipVoteBase;
			tmpCanVoteRemain = tmpCanVoteCount%tmpVipVoteBase;
			if (tmpCanVoteRemain > 0){
				tmpCanVoteTimes = tmpCanVoteTimes + 1;
			}
			tmpRemainVoteCount = tmpCanVoteCount - tmpHaveVotes;
			if (tmpRemainVoteCount < 0) tmpRemainVoteCount = 0;
			tmpRemainVoteTimes = tmpRemainVoteCount/tmpVipVoteBase;
			tmpRemainVoteRemain = tmpRemainVoteCount%tmpVipVoteBase;
			if (tmpRemainVoteRemain > 0){
				tmpRemainVoteTimes = tmpRemainVoteTimes + 1;
			}
			tmpTVoteUser.setVoteQrcode(tmpQrVotes);
			tmpTVoteUser.setVoteBase(tmpBeVotes);
			tmpTVoteUser.setCanVotes(tmpCanVoteCount);
			tmpTVoteUser.setRemainVotes(tmpRemainVoteCount);
			tmpTVoteUser.setUpdateTime(new Date());
			tmpTVoteUser.setEpgType(epgType);
			entityDao.update(tmpTVoteUser);
			
			resp.setCanVoteCount(tmpCanVoteCount);
			resp.setRemainVoteCount(tmpRemainVoteCount);
			resp.setCanVoteTimes(tmpCanVoteTimes);
			resp.setRemainVoteTimes(tmpRemainVoteTimes);
			logger.info(resultDesc);
		}catch(Exception e){
			resultDesc = String.format("获取用户[%s]投票计数失败，程序异常！", userID);
			logger.error(resultDesc, e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
	}

	public GetDayVoteRecordCountResp getDayVoteRecordCount(String voteCode, String userID, String playerCode) {
		
		GetDayVoteRecordCountResp resp = new GetDayVoteRecordCountResp();
		resp.setDayVoteCount(0);
		String resultDesc = String.format("获取用户[%s]当天投票记录计数成功！", userID);
		try{
			if (StringUtils.isBlank(userID)){
				resultDesc = String.format("获取用户[%s]当天投票记录计数失败，用户账号[%s]非法！", userID, voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			if (StringUtils.isBlank(voteCode)){
				resultDesc = String.format("获取用户[%s]当天投票记录计数失败，投票活动编码[%s]非法！", userID, voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("获取用户[%s]当天投票记录计数失败，投票活动编码[%s]非法！", userID, voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			String currentDate = DateFormatUtil.format(new Date(), "yyyy-MM-dd");
			//当天普通投票
			String tmpSql = "select count(o.ID) as count from t_vote_record_day o where o.VOTE_ID=? and o.USER_ID=? " +
					"and date_format(o.CREATE_TIME,'%Y-%m-%d')=? and o.USER_TYPE=?";
			List<Map<String, Object>> tmpListMapCount;
			if (!StringUtils.isBlank(playerCode)){
				tmpSql = "select count(o.ID) as count from t_vote_record_day o where o.VOTE_ID=? and o.USER_ID=? " +
						"and date_format(o.CREATE_TIME,'%Y-%m-%d')=? and o.USER_TYPE=? and o.PLAYER_CODE=?";
				tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, new Object[]{tmpTVoteGame.getId(), userID, 
						currentDate, VoteUserType.NORMAL_USER.getType(), playerCode}, -1, -1);
			}else{
				tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, new Object[]{tmpTVoteGame.getId(), userID, 
						currentDate, VoteUserType.NORMAL_USER.getType()}, -1, -1);
			}
			
			if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
				Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
				BigInteger tmpBigVoteCount = (BigInteger) tmpMapCount.get("count");
				if (tmpBigVoteCount != null)
				resp.setDayVoteCount(tmpBigVoteCount.intValue());
			}
			//当天订购投票
			if (!StringUtils.isBlank(playerCode)){
				tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, new Object[]{tmpTVoteGame.getId(), userID, 
						currentDate, VoteUserType.ORDER_USER.getType(), playerCode}, -1, -1);
			}else{
				tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, new Object[]{tmpTVoteGame.getId(), userID, 
						currentDate, VoteUserType.ORDER_USER.getType()}, -1, -1);
			}
			
			if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
				Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
				BigInteger tmpBigVoteCount = (BigInteger) tmpMapCount.get("count");
				if (tmpBigVoteCount != null)
				resp.setDayOrderVoteCount(tmpBigVoteCount.intValue());
			}
			logger.info(resultDesc);
		}catch(Exception e){
			resultDesc = String.format("获取用户[%s]当天投票记录计数失败，程序异常！", userID);
			logger.error(resultDesc, e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
	}
	
	public GetPeriodVoteRecordCountResp getPeriodVoteRecordCount(
			String voteCode, String userID, String playerCode) {
		GetPeriodVoteRecordCountResp resp = new GetPeriodVoteRecordCountResp();
		resp.setPeriodVoteCount(0);
		String resultDesc = String.format("获取用户[%s]活动期间投票记录计数成功！", userID);
		try{
			if (StringUtils.isBlank(userID)){
				resultDesc = String.format("获取用户[%s]活动期间投票记录计数失败，用户账号[%s]非法！", userID, voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			if (StringUtils.isBlank(voteCode)){
				resultDesc = String.format("获取用户[%s]活动期间投票记录计数失败，投票活动编码[%s]非法！", userID, voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("获取用户[%s]活动期间投票记录计数失败，投票活动编码[%s]非法！", userID, voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			//活动期间普通投票
			String tmpSql = "select count(o.ID) as count from t_vote_record o where o.VOTE_ID=? and o.USER_ID=? " +
					"and date_format(o.CREATE_TIME,'%Y-%m-%d %H:%i:%s')>=? and date_format(o.CREATE_TIME,'%Y-%m-%d %H:%i:%s')<=? and o.USER_TYPE=?";
			List<Map<String, Object>> tmpListMapCount;
			if (!StringUtils.isBlank(playerCode)){
				tmpSql = "select count(o.ID) as count from t_vote_record o where o.VOTE_ID=? and o.USER_ID=? " +
						"and date_format(o.CREATE_TIME,'%Y-%m-%d %H:%i:%s')>=? and date_format(o.CREATE_TIME,'%Y-%m-%d %H:%i:%s')<=? " +
						"and o.USER_TYPE=? and o.PLAYER_CODE=?";
				tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, new Object[]{tmpTVoteGame.getId(), userID, 
						tmpTVoteGame.getStartTime(), tmpTVoteGame.getEndTime(), VoteUserType.NORMAL_USER.getType(), playerCode}, -1, -1);
			}else{
				tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, new Object[]{tmpTVoteGame.getId(), userID, 
						tmpTVoteGame.getStartTime(), tmpTVoteGame.getEndTime(), VoteUserType.NORMAL_USER.getType()}, -1, -1);
			}
			
			if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
				Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
				BigInteger tmpBigVoteCount = (BigInteger) tmpMapCount.get("count");
				if (tmpBigVoteCount != null)
				resp.setPeriodVoteCount(tmpBigVoteCount.intValue());
			}
			//活动期间订购投票
			if (!StringUtils.isBlank(playerCode)){
				tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, new Object[]{tmpTVoteGame.getId(), userID, 
						tmpTVoteGame.getStartTime(), tmpTVoteGame.getEndTime(), VoteUserType.ORDER_USER.getType(), playerCode}, -1, -1);
			}else{
				tmpListMapCount = entityDao.getScrollDataBySql(tmpSql, new Object[]{tmpTVoteGame.getId(), userID, 
						tmpTVoteGame.getStartTime(), tmpTVoteGame.getEndTime(), VoteUserType.ORDER_USER.getType()}, -1, -1);
			}
			
			if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
				Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
				BigInteger tmpBigVoteCount = (BigInteger) tmpMapCount.get("count");
				if (tmpBigVoteCount != null)
				resp.setPeriodOrderVoteCount(tmpBigVoteCount.intValue());
			}
			logger.info(resultDesc);
		}catch(Exception e){
			resultDesc = String.format("获取用户[%s]活动期间投票记录计数失败，程序异常！", userID);
			logger.error(resultDesc, e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
	}
	
	@Transactional
	public LuckyVoteResp luckyVote(String playerCode, String userID, int epgType, int votes, String areaId){
		LuckyVoteResp resp = new LuckyVoteResp();
		Date currentDate = new Date();
		String resultDesc = String.format("[%s]投票成功！",userID);;
		try{
			//验证账号是否非空
			if (StringUtils.isBlank(userID)){
				resultDesc = "投票失败，用户账号非法！";
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			if (epgType > EpgType.FOURK.getType()){
				resultDesc = String.format("[%s]投票失败，epgType[%s]非法！", userID, epgType);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			//验证选手有效性
			ValidateRets<TVotePlayer>  tmpTVotePlayerValidateRets = votePlayerValidate.validate(new VotePlayerValidateArgs(resp, playerCode, currentDate));
			if (!tmpTVotePlayerValidateRets.isSuccess()) return resp;
			TVotePlayer tmpTVotePlayer = tmpTVotePlayerValidateRets.getEntity();
			//验证投票活动有效性
			TVoteGame tmpTVoteGame = tmpTVotePlayer.getTvoteGame();
			if (tmpTVoteGame == null){
				resultDesc = String.format("[%s]投票失败，投票活动不存在！",userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.VOTE_DOES_NOT_EXIST);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			String tmpVoteCode = tmpTVoteGame.getCode();
			//验证投票活动有效性
			ValidateRets<TVoteGame>  tmpTVoteGameValidateRets = voteGameValidate.validate(new VoteGameValidateArgs(resp, tmpTVoteGame, currentDate));
			if (!tmpTVoteGameValidateRets.isSuccess()) return resp;
			
			TVoteUser tmpTVoteUser = getTVoteUser(tmpTVoteGame, userID);
			
			//基本票数
			int tmpVoteBase = tmpTVoteGame.getVoteBase() == null ? 1 : tmpTVoteGame.getVoteBase();
			int tmpBeVotes = getVoteTimes(tmpTVoteGame, epgType) * tmpVoteBase;
			//二维码
			int tmpQrVotes = getQrVotes(tmpVoteCode, tmpTVoteGame.getQrcodeSwitch(), userID);
			
			//2017-05-03 增加区域下选手限制用户在有效期内的投票次数：0-不做限制
			int tmpVoteTimesLimit = getRegionVoteTimesLimit(tmpTVotePlayer.getTvoteRegion(), epgType);
			//2017-06-01 增加选手限制用户在有效期内的投票次数：0-不做限制
			int tmpPlayerVoteTimesLimit = getPlayerVoteTimesLimit(tmpTVotePlayer, epgType);
			//用户已投票数
			int tmpHaveVotes;
			//用户该区域已投票次数
			int tmpHaveVoteTimes = 0;
			//用户该选手已投票次数
			int tmpPlayerHaveVoteTimes = 0;
			//2017-05-03增加用户投票次数有效期范围限制：活动每天或活动期间
			int tmpVoteTimesValidity = tmpTVoteGame.getVoteTimesValidity() == null ? VoteTimesValidity.DAY.getValidity() : tmpTVoteGame.getVoteTimesValidity();
			if (tmpVoteTimesValidity == VoteTimesValidity.DAY.getValidity()){
				//2016-08-08 获取用户当天已投票数
				tmpHaveVotes = getDayVotes(tmpTVoteGame.getId(), userID);
				//2017-05-03获取用户当天该区域已投票次数
				if (tmpVoteTimesLimit > 0){
					tmpHaveVoteTimes = getDayRegionVoteTimes(tmpTVoteGame, tmpTVotePlayer.getTvoteRegion(), userID);
				}
				//2017-06-01获取用户当天该选手已投票次数
				if (tmpPlayerVoteTimesLimit > 0){
					tmpPlayerHaveVoteTimes = getDayPlayerVoteTimes(tmpTVoteGame, tmpTVotePlayer, userID);
				}
			}else{
				//2017-05-03获取活动期间用户已投票数
				tmpHaveVotes = getPeriodVotes(tmpTVoteGame, userID);
				//2017-05-03获取用户活动期间该区域已投票次数
				if (tmpVoteTimesLimit > 0){
					tmpHaveVoteTimes = getPeriodRegionVoteTimes(tmpTVoteGame, tmpTVotePlayer.getTvoteRegion(), userID);
				}
				//2017-06-01获取用户活动期间该选手已投票次数
				if (tmpPlayerVoteTimesLimit > 0){
					tmpPlayerHaveVoteTimes = getPeriodPlayerVoteTimes(tmpTVoteGame, tmpTVotePlayer, userID);
				}
			}
			//2017-06-27自定义每次投票产生票数
			if (votes < 0) votes = 0;
			
			int tmpCanVoteCount;
			int tmpCanVoteTimes;
			int tmpCanVoteRemain;
			int tmpRemainVoteCount;
			int tmpRemainVoteTimes;
			int tmpRemainVoteRemain;
			int tmpUserVotes;
			if (tmpTVoteUser == null){
				tmpCanVoteCount = tmpBeVotes + tmpQrVotes;
				tmpCanVoteTimes = tmpCanVoteCount/tmpVoteBase;
				tmpCanVoteRemain = tmpCanVoteCount%tmpVoteBase;
				if (tmpCanVoteRemain > 0){
					tmpCanVoteTimes = tmpCanVoteTimes + 1;
				}
				tmpRemainVoteCount = tmpCanVoteCount - tmpHaveVotes;
				if (tmpRemainVoteCount < 0){
					tmpRemainVoteCount = 0;
				}
				tmpRemainVoteTimes = tmpRemainVoteCount/tmpVoteBase;
				tmpRemainVoteRemain = tmpRemainVoteCount%tmpVoteBase;
				if (tmpRemainVoteRemain > 0){
					tmpRemainVoteTimes = tmpRemainVoteTimes + 1;
				}
				tmpUserVotes = tmpVoteBase;
				resp.setCanVoteCount(tmpCanVoteCount);
				resp.setCanVoteTimes(tmpCanVoteTimes);
				if (tmpRemainVoteTimes == 0){
					resultDesc = String.format("[%s]投票失败，用户只能投[%s]票，请订购产品获取更多投票机会！",userID, tmpCanVoteCount);
					logger.error(resultDesc);
					resp.setResultCode(LuckyVoteResultCode.CHANCE_HAS_BEEN_FINISHED);
					resp.setResultDesc(resultDesc);
					resp.setRemainVoteCount(0);
					resp.setRemainVoteTimes(0);
					return resp;
				}else{
					//加入区域限制判断
					if (tmpVoteTimesLimit > 0 && tmpVoteTimesLimit <= tmpHaveVoteTimes){
						String tmpRegionName = null;
						if (tmpTVotePlayer.getTvoteRegion() != null){
							tmpRegionName = tmpTVotePlayer.getTvoteRegion().getName();
						}
						resultDesc = String.format("[%s]投票失败，只能给区域[%s]投[%s]票！",userID, tmpRegionName, tmpVoteTimesLimit);
						logger.error(resultDesc);
						resp.setResultCode(LuckyVoteResultCode.VOTE_TO_REGION_CHANCE_HAS_BEEN_FINISHED);
						resp.setResultDesc(resultDesc);
						resp.setRemainVoteCount(tmpRemainVoteCount);
						resp.setRemainVoteTimes(tmpRemainVoteTimes);
						return resp;
					}
					//加入选手限制
					if (tmpPlayerVoteTimesLimit > 0 && tmpPlayerVoteTimesLimit <= tmpPlayerHaveVoteTimes){
						resultDesc = String.format("[%s]投票失败，只能给选手[%s]投[%s]票！",userID, tmpTVotePlayer.getName(), tmpPlayerVoteTimesLimit);
						logger.error(resultDesc);
						resp.setResultCode(LuckyVoteResultCode.VOTE_TO_PLAYER_CHANCE_HAS_BEEN_FINISHED);
						resp.setResultDesc(resultDesc);
						resp.setRemainVoteCount(tmpRemainVoteCount);
						resp.setRemainVoteTimes(tmpRemainVoteTimes);
						return resp;
					}
					if (tmpVoteBase > tmpRemainVoteCount){
						tmpUserVotes = tmpRemainVoteRemain;
						resp.setRemainVoteCount(0);
						resp.setRemainVoteTimes(0);
						//2017-06-27自定义每次投票产生票数
						if (votes > 0){
							if (tmpUserVotes > votes){
								tmpUserVotes = votes;
								resp.setRemainVoteCount(tmpUserVotes - votes);
								resp.setRemainVoteTimes(1);
							}
						}
					}else{
						resp.setRemainVoteCount(tmpRemainVoteCount - tmpVoteBase);
						resp.setRemainVoteTimes(tmpRemainVoteTimes - 1);
						//2017-06-27自定义每次投票产生票数
						if (votes > 0){
							if (tmpRemainVoteCount > votes){
								tmpUserVotes = votes;
								resp.setRemainVoteCount(tmpRemainVoteCount - votes);
								resp.setRemainVoteTimes(tmpRemainVoteTimes - 1);
							}else if (tmpRemainVoteCount == votes){
								tmpUserVotes = votes;
								resp.setRemainVoteCount(0);
								resp.setRemainVoteTimes(0);
							}else {
								tmpUserVotes = tmpRemainVoteCount;
								resp.setRemainVoteCount(0);
								resp.setRemainVoteTimes(0);
							}
						}
					}
					int tmpPlayerVotes = addPlayerVote(tmpTVotePlayer, tmpUserVotes, currentDate);
					//新增投票记录
					addVoteRecord(tmpTVoteGame, tmpTVotePlayer, userID, tmpUserVotes, currentDate, 
							VoteUserType.NORMAL_USER.getType(), epgType, areaId);
					resp.setPlayerVotes(tmpPlayerVotes);
					return resp;
				}
			}
			if (tmpTVoteUser.getStatus() == 0){
				resultDesc = String.format("[%s]投票失败，用户被禁用投票！",userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.USER_VOTE_BEEN_DISABLED);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			//订购用户投票
			int tmpVipVoteBase = tmpTVoteGame.getVipVoteBase() == null ? 1 : tmpTVoteGame.getVipVoteBase();
			tmpCanVoteCount = tmpTVoteUser.getCanVotes() == null ? 0 : tmpTVoteUser.getCanVotes();
			int tmpSrcQrVotes = tmpTVoteUser.getVoteQrcode() == null ? 0 : tmpTVoteUser.getVoteQrcode();
			int tmpSrcBaseVotes = tmpTVoteUser.getVoteBase() == null ? 0 : tmpTVoteUser.getVoteBase();
			int tmpDivQrVotes = tmpQrVotes - tmpSrcQrVotes;
			int tmpDivBaseVotes = tmpBeVotes - tmpSrcBaseVotes;
			tmpCanVoteCount = tmpCanVoteCount + tmpDivQrVotes + tmpDivBaseVotes;
			if (tmpCanVoteCount < 0) tmpCanVoteCount = 0;
			tmpCanVoteTimes = tmpCanVoteCount/tmpVipVoteBase;
			tmpCanVoteRemain = tmpCanVoteCount%tmpVipVoteBase;
			if (tmpCanVoteRemain > 0){
				tmpCanVoteTimes = tmpCanVoteTimes + 1;
			}
			tmpRemainVoteCount = tmpCanVoteCount - tmpHaveVotes;
			resp.setCanVoteCount(tmpCanVoteCount);
			resp.setCanVoteTimes(tmpCanVoteTimes);
			if (tmpRemainVoteCount <= 0){
				resultDesc = String.format("[%s]投票失败，用户投票机会已用完，请订购产品获取更多投票机会！",userID);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.CHANCE_HAS_BEEN_FINISHED);
				resp.setResultDesc(resultDesc);
				resp.setRemainVoteCount(0);
				resp.setRemainVoteTimes(0);
				return resp;
			}
			tmpRemainVoteTimes = tmpRemainVoteCount/tmpVipVoteBase;
			tmpRemainVoteRemain = tmpRemainVoteCount%tmpVipVoteBase;
			if (tmpRemainVoteRemain > 0){
				tmpRemainVoteTimes = tmpRemainVoteTimes + 1;
			}
			
			//加入区域限制判断
			if (tmpVoteTimesLimit > 0 && tmpVoteTimesLimit <= tmpHaveVoteTimes){
				String tmpRegionName = null;
				if (tmpTVotePlayer.getTvoteRegion() != null){
					tmpRegionName = tmpTVotePlayer.getTvoteRegion().getName();
				}
				resultDesc = String.format("[%s]投票失败，只能给区域[%s]投[%s]票！",userID, tmpRegionName, tmpVoteTimesLimit);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.VOTE_TO_REGION_CHANCE_HAS_BEEN_FINISHED);
				resp.setResultDesc(resultDesc);
				resp.setRemainVoteCount(tmpRemainVoteCount);
				resp.setRemainVoteTimes(tmpRemainVoteTimes);
				return resp;
			}
			//加入选手限制
			if (tmpPlayerVoteTimesLimit > 0 && tmpPlayerVoteTimesLimit <= tmpPlayerHaveVoteTimes){
				resultDesc = String.format("[%s]投票失败，只能给选手[%s]投[%s]票！",userID, tmpTVotePlayer.getName(), tmpPlayerVoteTimesLimit);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.VOTE_TO_PLAYER_CHANCE_HAS_BEEN_FINISHED);
				resp.setResultDesc(resultDesc);
				resp.setRemainVoteCount(tmpRemainVoteCount);
				resp.setRemainVoteTimes(tmpRemainVoteTimes);
				return resp;
			}
			if (tmpVipVoteBase > tmpRemainVoteCount){
				tmpUserVotes = tmpRemainVoteCount;
				resp.setRemainVoteCount(0);
				resp.setRemainVoteTimes(0);
				//2017-06-27自定义每次投票产生票数
				if (votes > 0){
					if (tmpUserVotes > votes){
						tmpUserVotes = votes;
						resp.setRemainVoteCount(tmpUserVotes - votes);
						resp.setRemainVoteTimes(1);
					}
				}
			}else{
				tmpUserVotes = tmpVipVoteBase;
				resp.setRemainVoteCount(tmpRemainVoteCount - tmpVipVoteBase);
				resp.setRemainVoteTimes(tmpRemainVoteTimes - 1);
				//2017-06-27自定义每次投票产生票数
				if (votes > 0){
					if (tmpRemainVoteCount > votes){
						tmpUserVotes = votes;
						resp.setRemainVoteCount(tmpRemainVoteCount - votes);
						resp.setRemainVoteTimes(tmpRemainVoteTimes - 1);
					}else if (tmpRemainVoteCount == votes){
						tmpUserVotes = votes;
						resp.setRemainVoteCount(0);
						resp.setRemainVoteTimes(0);
					}else {
						tmpUserVotes = tmpRemainVoteCount;
						resp.setRemainVoteCount(0);
						resp.setRemainVoteTimes(0);
					}
				}
			}
			//新增参数选手票数
			int tmpPlayerVotes = addPlayerVote(tmpTVotePlayer, tmpUserVotes, currentDate);
			tmpTVoteUser.setVoteQrcode(tmpQrVotes);
			tmpTVoteUser.setVoteBase(tmpBeVotes);
			tmpTVoteUser.setCanVotes(tmpCanVoteCount);
			tmpTVoteUser.setRemainVotes(resp.getRemainVoteCount());
			tmpTVoteUser.setUpdateTime(currentDate);
			tmpTVoteUser.setEpgType(epgType);
			entityDao.update(tmpTVoteUser);
			//新增投票记录
			addVoteRecord(tmpTVoteGame, tmpTVotePlayer, userID, tmpUserVotes, currentDate, 
					VoteUserType.ORDER_USER.getType(), epgType, areaId);
			resp.setPlayerVotes(tmpPlayerVotes);
			logger.info(resultDesc);
		}catch(Exception e){
			resultDesc = String.format("[%s]投票失败，程序异常！",userID);
			logger.error(resultDesc , e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
	}
	
	private int addPlayerVote(TVotePlayer tvotePlayer, int votes, Date currentDate){
		int tmpVotes = tvotePlayer.getVotes() == null ? 1 : tvotePlayer.getVotes();
		tmpVotes = tmpVotes + votes;
		tvotePlayer.setVotes(tmpVotes);
		int tmpVoteOther = tvotePlayer.getVotesOther() == null ? 0 : tvotePlayer.getVotesOther();
		int tmpTotalVotes = tmpVotes + tmpVoteOther;
		tvotePlayer.setVotesTotal(tmpTotalVotes);
		tvotePlayer.setUpdateTime(currentDate);
		entityDao.update(tvotePlayer);
		return tmpTotalVotes;
	}
	
	private void addVoteRecord(TVoteGame tvoteGame,TVotePlayer tvotePlayer, String userId, Integer votes, Date currentDate, 
			int userType, int epgType, String areaId){
		TVoteRecord tmpTVoteRecord = new TVoteRecord();
		tmpTVoteRecord.setCpCode(tvoteGame.getCpCode());
		tmpTVoteRecord.setCreateTime(currentDate);
		tmpTVoteRecord.setPlayerCode(tvotePlayer.getCode());
		tmpTVoteRecord.setPlayerName(tvotePlayer.getName());
		tmpTVoteRecord.setTvoteGame(tvoteGame);
		tmpTVoteRecord.setUserId(userId);
		tmpTVoteRecord.setVotes(votes);
		tmpTVoteRecord.setUserType(userType);
		tmpTVoteRecord.setEpgType(epgType);
		tmpTVoteRecord.setAreaId(areaId);
		entityDao.saveObj(tmpTVoteRecord);
		
		TVoteRecordDay tmpTVoteRecordDay = new TVoteRecordDay();
		tmpTVoteRecordDay.setCpCode(tvoteGame.getCpCode());
		tmpTVoteRecordDay.setCreateTime(currentDate);
		tmpTVoteRecordDay.setPlayerCode(tvotePlayer.getCode());
		tmpTVoteRecordDay.setPlayerName(tvotePlayer.getName());
		tmpTVoteRecordDay.setTvoteGame(tvoteGame);
		tmpTVoteRecordDay.setUserId(userId);
		tmpTVoteRecordDay.setVotes(votes);
		tmpTVoteRecordDay.setUserType(userType);
		tmpTVoteRecordDay.setEpgType(epgType);
		tmpTVoteRecordDay.setAreaId(areaId);
		entityDao.saveObj(tmpTVoteRecordDay);
	}
	
	public GetVotePlayerListResp getMonthChampion(String voteCode, int whichYear, int firstIndex, int maxResult) {
		
		GetVotePlayerListResp resp = new GetVotePlayerListResp();
		String resultDesc = String.format("投票活动[%s]获取[%s]年月冠军榜成功！", voteCode, whichYear);
		try{
			if (StringUtils.isBlank(voteCode)){
				resultDesc = String.format("投票活动[%s]获取[%s]年月冠军榜失败，编码非法！", voteCode, whichYear);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("投票活动[%s]获取[%s]年月冠军榜失败，编码非法！", voteCode, whichYear);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			StringBuilder tmpCountSB = new StringBuilder();
			tmpCountSB.append("select count(t1.VOTE_MONTH) as count from t_vote_summary t1,")
					  .append("(select VOTE_MONTH, max(VOTES_TOTAL) as maxVotes from t_vote_summary ")
					  .append("where VOTE_ID=? and VOTE_YEAR=? group by VOTE_MONTH) as t2 ")
					  .append("where t1.VOTE_MONTH=t2.VOTE_MONTH and t1.VOTES_TOTAL=t2.maxVotes");
			
			List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataBySql(tmpCountSB.toString(), 
					new Object[]{tmpTVoteGame.getId(), whichYear}, -1, -1);
			int tmpIntCount = 0;
			if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
				Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
				BigInteger tmpLongCount = (BigInteger) tmpMapCount.get("count");
				tmpIntCount = tmpLongCount.intValue();
			}
			if (tmpIntCount <= 0) return resp;
			
			StringBuilder tmpInfoSB = new StringBuilder();
			tmpInfoSB.append("select t1.PLAYER_CODE as playerCode,t1.VOTE_YEAR as voteYear,t1.VOTE_MONTH as voteMonth,")
					  .append("t1.VOTES as votes,t1.VOTES_OTHER as votesOther,t1.VOTES_TOTAL as votesTotal from t_vote_summary t1, ")
					  .append("(select VOTE_MONTH, max(VOTES_TOTAL) as maxVotes from t_vote_summary ")
					  .append("where VOTE_ID=? and VOTE_YEAR=? group by VOTE_MONTH) as t2 ")
					  .append("where t1.VOTE_MONTH=t2.VOTE_MONTH and t1.VOTES_TOTAL=t2.maxVotes order by t1.VOTE_MONTH asc");
			
			List<Map<String, Object>> tmpListMap = entityDao.getScrollDataBySql(tmpInfoSB.toString(), 
					new Object[]{tmpTVoteGame.getId(), whichYear}, firstIndex, maxResult);
			if (tmpListMap == null || tmpListMap.isEmpty()) return  resp;
			resp.setRecordSize(tmpIntCount);
			
			List<GetVotePlayerListResp.VotePlayerInfo> tmpListVotePlayerInfo = new ArrayList<GetVotePlayerListResp.VotePlayerInfo>();
			GetVotePlayerListResp.VotePlayerInfo tmpVotePlayerInfo;
			String tmpPlayerCode;
			TVotePlayer tmpTVotePlayer;
			for (Map<String, Object> tmpMap : tmpListMap){
				tmpPlayerCode = (String) tmpMap.get("playerCode");
				tmpTVotePlayer = entityDao.get(TVotePlayer.class, "o.code=?1", new Object[]{tmpPlayerCode});
				if (tmpTVotePlayer == null) continue;
				tmpVotePlayerInfo = new GetVotePlayerListResp.VotePlayerInfo();
				if (!StringUtils.isBlank(tmpTVotePlayer.getAvatar())){
					tmpVotePlayerInfo.setAvatar(getHttpUrl(tmpTVotePlayer.getAvatar()));
				}
				if (!StringUtils.isBlank(tmpTVotePlayer.getAvatarhd())){
					tmpVotePlayerInfo.setAvatarhd(getHttpUrl(tmpTVotePlayer.getAvatarhd()));
				}
				tmpVotePlayerInfo.setCode(tmpTVotePlayer.getCode());
				tmpVotePlayerInfo.setName(tmpTVotePlayer.getName());
				tmpVotePlayerInfo.setProfile(tmpTVotePlayer.getProfile());
				tmpVotePlayerInfo.setRelCode(tmpTVotePlayer.getRelCode());
				tmpVotePlayerInfo.setRelCodehd(tmpTVotePlayer.getRelCodehd());
				tmpVotePlayerInfo.setVotes((Integer)tmpMap.get("votes"));
				tmpVotePlayerInfo.setVotesOther((Integer)tmpMap.get("votesOther"));
				tmpVotePlayerInfo.setVotesTotal((Integer)tmpMap.get("votesTotal"));
				tmpVotePlayerInfo.setVoteMonth((Integer)tmpMap.get("voteMonth"));
				tmpVotePlayerInfo.setVoteYear((Integer)tmpMap.get("voteYear"));
				tmpListVotePlayerInfo.add(tmpVotePlayerInfo);
			}
			resp.setVotePlayerInfos(tmpListVotePlayerInfo);
			logger.info(resultDesc);
		}catch(Exception e){
			resultDesc = String.format("投票活动[%s]获取参赛选手失败，程序异常！", voteCode);
			logger.error(resultDesc, e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
	}

	public GetVotePlayerListResp getMonthRank(String voteCode, int whichYear, int whichMonth, int firstIndex, int maxResult) {
		
		GetVotePlayerListResp resp = new GetVotePlayerListResp();
		String resultDesc = String.format("投票活动[%s]获取[%s]年[%s]月排行榜成功！", voteCode, whichYear, whichMonth);
		try{
			if (StringUtils.isBlank(voteCode)){
				resultDesc = String.format("投票活动[%s]获取[%s]年[%s]月排行榜失败，编码非法！", voteCode, whichYear, whichMonth);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("投票活动[%s]获取[%s]年[%s]月排行榜失败，编码非法！", voteCode, whichYear, whichMonth);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			String tmpSql = "select count(o.id) as count from TVoteSummary o where o.tvoteGame.code=? and o.voteYear=? and o.voteMonth=?";
			List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{voteCode, whichYear, whichMonth}, -1, -1);
			int tmpIntCount = 0;
			if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
				Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
				Long tmpLongCount = (Long) tmpMapCount.get("count");
				tmpIntCount = tmpLongCount.intValue();
			}
			if (tmpIntCount <= 0) return resp;
			
			LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
			orderBy.put("votes", "desc");
			List<TVoteSummary> tmpListTVoteSummary = entityDao.getScrollData(TVoteSummary.class, firstIndex, maxResult, 
					"o.tvoteGame.code=?1 and o.voteYear=?2 and o.voteMonth=?3", 
					new Object[]{voteCode, whichYear, whichMonth}, orderBy);
			if (tmpListTVoteSummary == null || tmpListTVoteSummary.isEmpty()) return resp;
			resp.setRecordSize(tmpIntCount);
			
			List<GetVotePlayerListResp.VotePlayerInfo> tmpListVotePlayerInfo = new ArrayList<GetVotePlayerListResp.VotePlayerInfo>();
			GetVotePlayerListResp.VotePlayerInfo tmpVotePlayerInfo;
			TVotePlayer tmpTVotePlayer;
			for (TVoteSummary tmpTVoteSummary : tmpListTVoteSummary){
				tmpTVotePlayer = entityDao.get(TVotePlayer.class, "o.code=?1", new Object[]{tmpTVoteSummary.getPlayerCode()});
				if (tmpTVotePlayer == null) continue;
				tmpVotePlayerInfo = new GetVotePlayerListResp.VotePlayerInfo();
				if (!StringUtils.isBlank(tmpTVotePlayer.getAvatar())){
					tmpVotePlayerInfo.setAvatar(getHttpUrl(tmpTVotePlayer.getAvatar()));
				}
				if (!StringUtils.isBlank(tmpTVotePlayer.getAvatarhd())){
					tmpVotePlayerInfo.setAvatarhd(getHttpUrl(tmpTVotePlayer.getAvatarhd()));
				}
				tmpVotePlayerInfo.setCode(tmpTVotePlayer.getCode());
				tmpVotePlayerInfo.setName(tmpTVotePlayer.getName());
				tmpVotePlayerInfo.setProfile(tmpTVotePlayer.getProfile());
				tmpVotePlayerInfo.setRelCode(tmpTVotePlayer.getRelCode());
				tmpVotePlayerInfo.setRelCodehd(tmpTVotePlayer.getRelCodehd());
				tmpVotePlayerInfo.setVotes(tmpTVoteSummary.getVotes());
				tmpVotePlayerInfo.setVotesOther(tmpTVoteSummary.getVotesOther());
				tmpVotePlayerInfo.setVotesTotal(tmpTVoteSummary.getVotesTotal());
				tmpVotePlayerInfo.setVoteMonth(tmpTVoteSummary.getVoteMonth());
				tmpVotePlayerInfo.setVoteYear(tmpTVoteSummary.getVoteYear());
				tmpListVotePlayerInfo.add(tmpVotePlayerInfo);
			}
			resp.setVotePlayerInfos(tmpListVotePlayerInfo);
			logger.info(resultDesc);
		}catch(Exception e){
			resultDesc = String.format("投票活动[%s]获取[%s]年[%s]月排行榜失败，程序异常！", voteCode, whichYear, whichMonth);
			logger.error(resultDesc, e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
		
	}
	
	public GetVotePlayerListResp getWeekChampion(String voteCode, int firstIndex, int maxResult){
		GetVotePlayerListResp resp = new GetVotePlayerListResp();
		String resultDesc = String.format("投票活动[%s]获取周排行榜成功！", voteCode);
		try{
			if (StringUtils.isBlank(voteCode)){
				resultDesc = String.format("投票活动[%s]获取周排行榜失败，编码非法！", voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("投票活动[%s]获取周排行榜失败，编码非法！", voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			String tmpSql = "select count(o.id) as count from TVoteWeekChamp o where o.tvoteGame.code=?";
			List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{voteCode}, -1, -1);
			int tmpIntCount = 0;
			if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
				Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
				Long tmpLongCount = (Long) tmpMapCount.get("count");
				tmpIntCount = tmpLongCount.intValue();
			}
			if (tmpIntCount <= 0) return resp;
			
			tmpSql = getVoteWeekChampSelectSql() + " where o.VOTE_ID=" + tmpTVoteGame.getId() + " order by o.ANNOUNCE_STATUS desc,o.ANNOUNCE_TIME desc";
			List<Map<String, Object>> tmpListMap = entityDao.getScrollDataBySql(tmpSql, null, firstIndex, maxResult);
			if (tmpListMap == null || tmpListMap.isEmpty()){
				return resp;
			}
			List<GetVotePlayerListResp.VotePlayerInfo> tmpListVotePlayerInfo = new ArrayList<GetVotePlayerListResp.VotePlayerInfo>();
			GetVotePlayerListResp.VotePlayerInfo tmpVotePlayerInfo;
			SimpleDateFormat tmpFormat1 = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat tmpFormat2 = new SimpleDateFormat("M.d");
			String tmpStr;
			tmpSql = "select o.CODE as code,o.NAME as name from t_vote_region o where o.ID=%s";
			List<Map<String, Object>> tmpListRgMap;
			Map<String, Object> tmpRgMap;
			BigInteger tmpBig;
			for (Map<String, Object> tmpMap : tmpListMap){
				tmpVotePlayerInfo = new GetVotePlayerListResp.VotePlayerInfo();
				tmpStr = (String) tmpMap.get("avatar");
				if (!StringUtils.isBlank(tmpStr)){
					tmpVotePlayerInfo.setAvatar(getHttpUrl(tmpStr));
				}
				tmpStr = (String) tmpMap.get("avatarhd");
				if (!StringUtils.isBlank(tmpStr)){
					tmpVotePlayerInfo.setAvatarhd(getHttpUrl(tmpStr));
				}
				tmpVotePlayerInfo.setCode((String) tmpMap.get("code"));
				tmpVotePlayerInfo.setName((String) tmpMap.get("name"));
				tmpVotePlayerInfo.setProfile((String) tmpMap.get("profile"));
				tmpVotePlayerInfo.setRelCode((String) tmpMap.get("relCode"));
				tmpVotePlayerInfo.setRelCodehd((String) tmpMap.get("relCodehd"));
				
				tmpStr = (String) tmpMap.get("announceTime");
				if (!StringUtils.isBlank(tmpStr)){
					tmpVotePlayerInfo.setAnnounceTime(tmpStr);
					tmpVotePlayerInfo.setAnnounceTimeFormat(tmpFormat2.format(tmpFormat1.parse(tmpStr)));
				}
				tmpVotePlayerInfo.setGameSite((String) tmpMap.get("gameSite"));
				tmpVotePlayerInfo.setAnnounceStatus((Integer) tmpMap.get("announceStatus"));
				tmpVotePlayerInfo.setVotes((Integer) tmpMap.get("votes"));
				
				tmpBig = (BigInteger) tmpMap.get("regionId");
				if (tmpBig != null){
					tmpListRgMap = entityDao.getScrollDataBySql(String.format(tmpSql, tmpBig.longValue()), null, -1, -1);
					if (tmpListRgMap != null && !tmpListRgMap.isEmpty()){
						tmpRgMap = tmpListRgMap.get(0);
						tmpVotePlayerInfo.setRegionCode((String) tmpRgMap.get("code"));
						tmpVotePlayerInfo.setRegionName((String) tmpRgMap.get("name"));
					}
				}
				tmpVotePlayerInfo.setPlayerNo((String) tmpMap.get("playerNo"));
				tmpVotePlayerInfo.setStatus((Integer) tmpMap.get("status"));
				tmpListVotePlayerInfo.add(tmpVotePlayerInfo);
			}
			
			resp.setVotePlayerInfos(tmpListVotePlayerInfo);
			resp.setRecordSize(tmpIntCount);
			logger.info(resultDesc);
		}catch(Exception e){
			resultDesc = String.format("投票活动[%s]获取周排行榜失败，程序异常！", voteCode);
			logger.error(resultDesc, e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
	}

	public GetVotePlayerResp getVotePlayer(String playerCode, int type) {
		
		String resultDesc = String.format("获取选手[%s]信息成功！", playerCode);
		GetVotePlayerResp resp = new GetVotePlayerResp();
		if (StringUtils.isBlank(playerCode)){
			resultDesc = String.format("获取选手[%s]信息失败，选手编码非法！", playerCode);
			logger.error(resultDesc);
			resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc(resultDesc);
			return resp;
		}
		
		List<Map<String, Object>> tmpListMap;
		Map<String, Object> tmpMap;
		String tmpSql;
		String tmpStr;
		BigInteger tmpBig;
		List<Map<String, Object>> tmpListRgMap;
		Map<String, Object> tmpRgMap;
		try {
			//获取选手信息
			if (type == 0){
				tmpSql = getVotePlayerSelectSql() + " where o.CODE=?";
				tmpListMap = entityDao.getScrollDataBySql(tmpSql, new Object[]{playerCode}, -1, -1);
				if (tmpListMap == null || tmpListMap.isEmpty()){
					resultDesc = String.format("获取选手[%s]信息失败，选手编码不存在！", playerCode);
					logger.error(resultDesc);
					resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
					resp.setResultDesc(resultDesc);
					return resp;
				}
				tmpMap = tmpListMap.get(0);
				tmpStr = (String) tmpMap.get("avatar");
				if (!StringUtils.isBlank(tmpStr)){
					resp.setAvatar(getHttpUrl(tmpStr));
				}
				tmpStr = (String) tmpMap.get("avatarhd");
				if (!StringUtils.isBlank(tmpStr)){
					resp.setAvatarhd(getHttpUrl(tmpStr));
				}
				resp.setCode((String) tmpMap.get("code"));
				resp.setName((String) tmpMap.get("name"));
				resp.setProfile((String) tmpMap.get("profile"));
				resp.setRelCode((String) tmpMap.get("relCode"));
				resp.setRelCodehd((String) tmpMap.get("relCodehd"));
				resp.setVotes((Integer) tmpMap.get("votes"));
				resp.setVotesOther((Integer) tmpMap.get("votesOther"));
				resp.setVotesTotal((Integer) tmpMap.get("votesTotal"));
				tmpBig = (BigInteger) tmpMap.get("regionId");
				if (tmpBig != null){
					tmpSql = "select o.CODE as code,o.NAME as name from t_vote_region o where o.ID=" + tmpBig.longValue();
					tmpListRgMap = entityDao.getScrollDataBySql(tmpSql, null, -1, -1);
					if (tmpListRgMap != null && !tmpListRgMap.isEmpty()){
						tmpRgMap = tmpListRgMap.get(0);
						resp.setRegionCode((String) tmpRgMap.get("code"));
						resp.setRegionName((String) tmpRgMap.get("name"));
					}
				}
				resp.setPlayerNo((String) tmpMap.get("playerNo"));
				resp.setStatus((Integer) tmpMap.get("status"));
				resp.setStartTime(DateFormatUtil.format((Date) tmpMap.get("startTime")));
				resp.setEndTime(DateFormatUtil.format((Date) tmpMap.get("endTime")));
				
				logger.info(resultDesc);
			//获取冠军信息
			}else {
				tmpSql = getVoteWeekChampSelectSql() + " where o.CODE=?";
				tmpListMap = entityDao.getScrollDataBySql(tmpSql, new Object[]{playerCode}, -1, -1);
				if (tmpListMap == null || tmpListMap.isEmpty()){
					resultDesc = String.format("获取冠军[%s]信息失败，选手编码不存在！", playerCode);
					logger.error(resultDesc);
					resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
					resp.setResultDesc(resultDesc);
					return resp;
				}
				tmpMap = tmpListMap.get(0);
				tmpStr = (String) tmpMap.get("avatar");
				if (!StringUtils.isBlank(tmpStr)){
					resp.setAvatar(getHttpUrl(tmpStr));
				}
				tmpStr = (String) tmpMap.get("avatarhd");
				if (!StringUtils.isBlank(tmpStr)){
					resp.setAvatarhd(getHttpUrl(tmpStr));
				}
				resp.setCode((String) tmpMap.get("code"));
				resp.setName((String) tmpMap.get("name"));
				resp.setProfile((String) tmpMap.get("profile"));
				resp.setRelCode((String) tmpMap.get("relCode"));
				resp.setRelCodehd((String) tmpMap.get("relCodehd"));
				resp.setVotes((Integer) tmpMap.get("votes"));
				tmpStr = (String) tmpMap.get("announceTime");
				if (!StringUtils.isBlank(tmpStr)){
					SimpleDateFormat tmpFormat1 = new SimpleDateFormat("yyyy-MM-dd");
					SimpleDateFormat tmpFormat2 = new SimpleDateFormat("M.d");
					resp.setAnnounceTime(tmpStr);
					resp.setAnnounceTimeFormat(tmpFormat2.format(tmpFormat1.parse(tmpStr)));
				}
				resp.setGameSite((String) tmpMap.get("gameSite"));
				resp.setAnnounceStatus((Integer) tmpMap.get("announceStatus"));
				tmpBig = (BigInteger) tmpMap.get("regionId");
				if (tmpBig != null){
					tmpSql = "select o.CODE as code,o.NAME as name from t_vote_region o where o.ID=" + tmpBig.longValue();
					tmpListRgMap = entityDao.getScrollDataBySql(tmpSql, null, -1, -1);
					if (tmpListRgMap != null && !tmpListRgMap.isEmpty()){
						tmpRgMap = tmpListRgMap.get(0);
						resp.setRegionCode((String) tmpRgMap.get("code"));
						resp.setRegionName((String) tmpRgMap.get("name"));
					}
				}
				resp.setPlayerNo((String) tmpMap.get("playerNo"));
				resp.setStatus((Integer) tmpMap.get("status"));
				
				resultDesc = String.format("获取冠军[%s]信息成功！", playerCode);
				logger.info(resultDesc);
			}
			
		} catch (Exception e) {
			logger.error("程序异常，" + e.getMessage(), e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("程序异常");
		}
		return resp;
		
	}
	
	public GetVoteRegionListResp getVoteRegionList(String regionCode, int retVotes, String voteCode, int status, int firstIndex, int maxResult) {
		
		GetVoteRegionListResp resp = new GetVoteRegionListResp();
		String resultDesc = String.format("获取区域[%s]列表成功！", regionCode);
		if (StringUtils.isBlank(regionCode)){
			resultDesc = String.format("获取区域[%s]列表失败，编码非法！", regionCode);
			logger.error(resultDesc);
			resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc(resultDesc);
			return resp;
		}
		try {
			String tmpVoteRegionSelectSql = getVoteRegionSelectSql();
			
			String tmpSql = tmpVoteRegionSelectSql + " where o.CODE=?";
			List<Map<String, Object>> tmpListMap = entityDao.getScrollDataBySql(tmpSql, new Object[]{regionCode}, -1, -1);
			if (tmpListMap == null || tmpListMap.isEmpty()){
				resultDesc = String.format("获取区域[%s]列表失败，区域不存在！", regionCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			BigInteger tmpBig = (BigInteger)tmpListMap.get(0).get("id");
			Long tmpParentId = 0L;
			if (tmpBig != null){
				tmpParentId = tmpBig.longValue();
			}
			
			
			tmpSql = "select count(o.id) as count from TVoteRegion o where o.tvoteRegion.code=?";
			String tmpWhereSql = " where o.PARENT_ID=" + tmpParentId;
			if (status == 0){
				tmpSql = tmpSql + " and o.status=1";
				tmpWhereSql = tmpWhereSql + " and o.STATUS=1";
			}else if (status == 1){
				tmpSql = tmpSql + " and o.status=0";
				tmpWhereSql = tmpWhereSql + " and o.STATUS=0";
			}
			List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{regionCode}, -1, -1);
			int tmpIntCount = 0;
			if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
				Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
				Long tmpLongCount = (Long) tmpMapCount.get("count");
				tmpIntCount = tmpLongCount.intValue();
			}
			if(tmpIntCount <= 0) return resp;
			
			tmpSql = tmpVoteRegionSelectSql + tmpWhereSql + " order by o.SEQUENCE desc,o.CREATE_TIME desc";
			tmpListMap = entityDao.getScrollDataBySql(tmpSql, null, firstIndex, maxResult);
			if (tmpListMap == null || tmpListMap.isEmpty()){
				return resp;
			}
			
			List<GetVoteRegionListResp.VoteRegionInfo> tmpListVoteRegionInfo = new ArrayList<GetVoteRegionListResp.VoteRegionInfo>();
			GetVoteRegionListResp.VoteRegionInfo tmpVoteRegionInfo;
			for (Map<String, Object> tmpMap : tmpListMap){
				tmpVoteRegionInfo = new GetVoteRegionListResp.VoteRegionInfo();
				tmpBig = (BigInteger)tmpMap.get("id");
				if (tmpBig != null){
					tmpVoteRegionInfo.setId(tmpBig.longValue());
				}
				tmpVoteRegionInfo.setCode((String)tmpMap.get("code"));
				tmpBig = (BigInteger)tmpMap.get("cpCode");
				if (tmpBig != null){
					tmpVoteRegionInfo.setCpCode(tmpBig.longValue());
				}
				tmpVoteRegionInfo.setName((String)tmpMap.get("name"));
				tmpVoteRegionInfo.setOriginalCode((String)tmpMap.get("originalCode"));
				tmpVoteRegionInfo.setSequence((Integer)tmpMap.get("sequence"));
				tmpVoteRegionInfo.setVoteTimes((Integer)tmpMap.get("voteTimes"));
				tmpVoteRegionInfo.setVoteTimes4k((Integer)tmpMap.get("voteTimes4k"));
				tmpVoteRegionInfo.setVoteTimesHd((Integer)tmpMap.get("voteTimesHd"));
				tmpVoteRegionInfo.setStartTime(DateFormatUtil.format((Date)tmpMap.get("startTime")));
				tmpVoteRegionInfo.setEndTime(DateFormatUtil.format((Date)tmpMap.get("endTime")));
				tmpVoteRegionInfo.setStatus((Integer)tmpMap.get("status"));
				tmpListVoteRegionInfo.add(tmpVoteRegionInfo);
			}
			
			if (retVotes != 0 && !StringUtils.isBlank(voteCode)){
				TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
				if (tmpTVoteGame != null){
					for (GetVoteRegionListResp.VoteRegionInfo tmp : tmpListVoteRegionInfo){
						int tmpVotes = getRegionPlayerVotes(tmpTVoteGame.getId(), tmp.getId());
						tmp.setVotes(tmpVotes);
					}
				}
				
			}
			resp.setVoteRegionInfos(tmpListVoteRegionInfo);
			resp.setRecordSize(tmpIntCount);
			logger.info(resultDesc);
		} catch (Exception e) {
			resultDesc = String.format("获取区域[%s]列表失败，程序异常！", regionCode);
			logger.error(resultDesc + e.getMessage(), e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
		
	}

	public GetVoteRegionDetailResp getVoteRegionDetail(String regionCode, int retVotes, String voteCode) {
		GetVoteRegionDetailResp resp = new GetVoteRegionDetailResp();
		String resultDesc = String.format("获取区域[%s]详情成功！", regionCode);
		if (StringUtils.isBlank(regionCode)){
			resultDesc = String.format("获取区域[%s]详情失败，编码非法！", regionCode);
			logger.error(resultDesc);
			resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc(resultDesc);
			return resp;
		}
		try {
			String tmpSql = getVoteRegionSelectSql() + " where o.CODE=?";
			List<Map<String, Object>> tmpListMap = entityDao.getScrollDataBySql(tmpSql, new Object[]{regionCode}, -1, -1);
			if (tmpListMap == null || tmpListMap.isEmpty()){
				resultDesc = String.format("获取区域[%s]详情失败，区域不存在！", regionCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			
			Map<String, Object> tmpMap = tmpListMap.get(0);
			BigInteger tmpBig = (BigInteger)tmpMap.get("id");
			if (tmpBig != null){
				resp.setId(tmpBig.longValue());
			}
			resp.setCode((String)tmpMap.get("code"));
			tmpBig = (BigInteger)tmpMap.get("cpCode");
			if (tmpBig != null){
				resp.setCpCode(tmpBig.longValue());
			}
			
			resp.setName((String)tmpMap.get("name"));
			resp.setOriginalCode((String)tmpMap.get("originalCode"));
			resp.setSequence((Integer)tmpMap.get("sequence"));
			
			resp.setVoteTimes((Integer)tmpMap.get("voteTimes"));
			resp.setVoteTimes4k((Integer)tmpMap.get("voteTimes4k"));
			resp.setVoteTimesHd((Integer)tmpMap.get("voteTimesHd"));
			resp.setStartTime(DateFormatUtil.format((Date)tmpMap.get("startTime")));
			resp.setEndTime(DateFormatUtil.format((Date)tmpMap.get("endTime")));
			resp.setStatus((Integer)tmpMap.get("status"));
			if (retVotes != 0 && !StringUtils.isBlank(voteCode)){
				TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
				if (tmpTVoteGame != null){
					int tmpVotes = getRegionPlayerVotes(tmpTVoteGame.getId(), resp.getId());
					resp.setVotes(tmpVotes);
				}
			}
			logger.info(resultDesc);
		} catch (Exception e) {
			resultDesc = String.format("获取区域[%s]详情失败，程序异常！", regionCode);
			logger.error(resultDesc + e.getMessage(), e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
		
	}
	@Transactional
	public VerifyInviteCodeResp verifyInviteCode(String qrCode, String inviteCode, String userID){
		
		VerifyInviteCodeResp resp = new VerifyInviteCodeResp();
		String resultDesc = String.format("用户[%s]验证二维码[%s]邀请码[%s]成功！", userID, qrCode, inviteCode);
		if (StringUtils.isBlank(StringUtils.trim(qrCode)) || StringUtils.isBlank(StringUtils.trim(inviteCode))
				|| StringUtils.isBlank(StringUtils.trim(userID))){
			resultDesc = String.format("用户[%s]验证二维码[%s]邀请码[%s]失败，参数非法！", userID, qrCode, inviteCode);
			logger.error(resultDesc);
			resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc(resultDesc);
			return resp;
		}
		try {
			TVoteQrcodeDetail tmpTVoteQrcodeDetail = entityDao.get(TVoteQrcodeDetail.class, 
					"o.tvoteQrcode.code=?1 and o.code=?2", new Object[]{qrCode, inviteCode});
			if (tmpTVoteQrcodeDetail == null){
				resultDesc = String.format("用户[%s]验证二维码[%s]邀请码[%s]失败，二维码或邀请码无效！", userID, qrCode, inviteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			TVoteQrcode tmpTVoteQrcode = tmpTVoteQrcodeDetail.getTvoteQrcode();
			if (tmpTVoteQrcode.getStatus() == 0){
				resultDesc = String.format("用户[%s]验证二维码[%s]邀请码[%s]失败，二维码已停用！", userID, qrCode, inviteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.QRCODE_HAS_BEEN_STOPPED);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			/*TVoteGame tmpTVoteGame = tmpTVoteQrcode.getTvoteGame();
			if (tmpTVoteGame == null){
				resultDesc = String.format("用户[%s]验证二维码[%s]邀请码[%s]失败，活动不存在！",userID, qrCode, inviteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.VOTE_DOES_NOT_EXIST);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			
			ValidateRets<TVoteGame>  tmpTVoteGameValidateRets = voteGameValidate.validate(new VoteGameValidateArgs(resp, tmpTVoteGame, new Date()));
			if (!tmpTVoteGameValidateRets.isSuccess()) return resp;*/
			
			if (tmpTVoteQrcodeDetail.getStatus() == 1){
				resultDesc = String.format("用户[%s]验证二维码[%s]邀请码[%s]失败，邀请码已被使用！", userID, qrCode, inviteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.INVITECODE_HAS_BEEN_USED);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			
			if (tmpTVoteQrcodeDetail.getUserId() != null && tmpTVoteQrcodeDetail.getUserId().equals(userID)){
				resultDesc = String.format("用户[%s]验证二维码[%s]邀请码[%s]失败，用户已验证！", userID, qrCode, inviteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.USER_HAS_VERIFIED_INVITECODE);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			
			/*TVoteQrcodeDetail tmpUserQrcodeDetail = entityDao.get(TVoteQrcodeDetail.class, 
					"o.tvoteQrcode.code=?1 and o.userId=?2", new Object[]{qrCode, userID});
			if (tmpUserQrcodeDetail != null){
				resultDesc = String.format("用户[%s]验证二维码[%s]邀请码[%s]失败，用户已验证[%s]！", userID, qrCode, 
						inviteCode, tmpUserQrcodeDetail.getCode());
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.USER_HAS_VERIFIED_INVITECODE);
				resp.setResultDesc(resultDesc);
				return resp;
			}*/
			tmpTVoteQrcodeDetail.setUserId(userID);
			tmpTVoteQrcodeDetail.setStatus(1);
			tmpTVoteQrcodeDetail.setBindTime(new Date());
			entityDao.update(tmpTVoteQrcodeDetail);
			logger.info(resultDesc);
		} catch (Exception e) {
			resultDesc = String.format("用户[%s]验证二维码[%s]邀请码[%s]失败，程序异常！", userID, qrCode, inviteCode);
			logger.error(resultDesc + e.getMessage(), e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
	}
	
	public GetVoteQrCodeDetailResp getVoteQrCodeDetail(String qrCode) {
		GetVoteQrCodeDetailResp resp = new GetVoteQrCodeDetailResp();
		String resultDesc = String.format("获取二维码[%s]详情成功！", qrCode);
		if (StringUtils.isBlank(qrCode)){
			resultDesc = String.format("获取二维码[%s]详情失败，编码非法！", qrCode);
			logger.error(resultDesc);
			resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc(resultDesc);
			return resp;
		}
		try {
			TVoteQrcode tmpTVoteQrcode = entityDao.get(TVoteQrcode.class, "o.code=?1 and o.status=1", new Object[]{qrCode});
			if (tmpTVoteQrcode == null){
				resultDesc = String.format("获取二维码[%s]详情失败，二维码不存在或者已停用！", qrCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			resp.setCode(qrCode);
			resp.setName(tmpTVoteQrcode.getName());
			if (!StringUtils.isBlank(tmpTVoteQrcode.getPicture())){
				resp.setPicture(getHttpUrl(tmpTVoteQrcode.getPicture()));
			}
			resp.setProfile(tmpTVoteQrcode.getProfile());
			resp.setVotes(tmpTVoteQrcode.getVotes());
			TVoteGame tmpTVoteGame = tmpTVoteQrcode.getTvoteGame();
			if (tmpTVoteGame != null){
				resp.setVoteCode(tmpTVoteGame.getCode());
				resp.setVoteName(tmpTVoteGame.getName());
			}
			resp.setCpCode(tmpTVoteQrcode.getCpCode());
			logger.info(resultDesc);
		} catch (Exception e) {
			resultDesc = String.format("获取二维码[%s]详情失败，程序异常！", qrCode);
			logger.error(resultDesc + e.getMessage(), e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
	}
	
	public GetVotePlayerResp searchVotePlayer(String voteCode, String playerNo, int type) {
		
		GetVotePlayerResp resp = new GetVotePlayerResp();
		String resultDesc = String.format("搜索选手信息失败，投票编码[%s]非法！", voteCode);
		if (StringUtils.isBlank(voteCode)){
			logger.error(resultDesc);
			resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc(resultDesc);
			return resp;
		}
		if (StringUtils.isBlank(playerNo)){
			resultDesc = String.format("搜索选手信息失败，选手编码[%s]非法！", playerNo);
			logger.error(resultDesc);
			resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc(resultDesc);
			return resp;
		}
		
		List<Map<String, Object>> tmpListMap;
		Map<String, Object> tmpMap;
		String tmpSql;
		String tmpStr;
		BigInteger tmpBig;
		List<Map<String, Object>> tmpListRgMap;
		Map<String, Object> tmpRgMap;
		try {
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("搜索选手信息失败，投票编码[%s]不存在！", voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.VOTE_DOES_NOT_EXIST);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			//获取选手信息
			if (type == 0){
				tmpSql = getVotePlayerSelectSql() + " where o.VOTE_ID=" + tmpTVoteGame.getId() + " and o.PLAYER_NO=?";
				tmpListMap = entityDao.getScrollDataBySql(tmpSql, new Object[]{playerNo}, -1, -1);
				if (tmpListMap == null || tmpListMap.isEmpty()){
					return resp;
				}
				tmpMap = tmpListMap.get(0);
				tmpStr = (String) tmpMap.get("avatar");
				if (!StringUtils.isBlank(tmpStr)){
					resp.setAvatar(getHttpUrl(tmpStr));
				}
				tmpStr = (String) tmpMap.get("avatarhd");
				if (!StringUtils.isBlank(tmpStr)){
					resp.setAvatarhd(getHttpUrl(tmpStr));
				}
				resp.setCode((String) tmpMap.get("code"));
				resp.setName((String) tmpMap.get("name"));
				resp.setProfile((String) tmpMap.get("profile"));
				resp.setRelCode((String) tmpMap.get("relCode"));
				resp.setRelCodehd((String) tmpMap.get("relCodehd"));
				resp.setVotes((Integer) tmpMap.get("votes"));
				resp.setVotesOther((Integer) tmpMap.get("votesOther"));
				resp.setVotesTotal((Integer) tmpMap.get("votesTotal"));
				tmpBig = (BigInteger) tmpMap.get("regionId");
				if (tmpBig != null){
					tmpSql = "select o.CODE as code,o.NAME as name from t_vote_region o where o.ID=" + tmpBig.longValue();
					tmpListRgMap = entityDao.getScrollDataBySql(tmpSql, null, -1, -1);
					if (tmpListRgMap != null && !tmpListRgMap.isEmpty()){
						tmpRgMap = tmpListRgMap.get(0);
						resp.setRegionCode((String) tmpRgMap.get("code"));
						resp.setRegionName((String) tmpRgMap.get("name"));
					}
				}
				resp.setPlayerNo((String) tmpMap.get("playerNo"));
				resp.setStatus((Integer) tmpMap.get("status"));
				resp.setStartTime(DateFormatUtil.format((Date) tmpMap.get("startTime")));
				resp.setEndTime(DateFormatUtil.format((Date) tmpMap.get("endTime")));
				logger.info("搜索选手信息成功");
			//获取冠军信息
			}else {
				tmpSql = getVoteWeekChampSelectSql() + " where o.VOTE_ID=" + tmpTVoteGame.getId() + " and o.PLAYER_NO=? ";
				tmpListMap = entityDao.getScrollDataBySql(tmpSql, new Object[]{playerNo}, -1, -1);
				if (tmpListMap == null || tmpListMap.isEmpty()){
					return resp;
				}
				tmpMap = tmpListMap.get(0);
				tmpStr = (String) tmpMap.get("avatar");
				if (!StringUtils.isBlank(tmpStr)){
					resp.setAvatar(getHttpUrl(tmpStr));
				}
				tmpStr = (String) tmpMap.get("avatarhd");
				if (!StringUtils.isBlank(tmpStr)){
					resp.setAvatarhd(getHttpUrl(tmpStr));
				}
				resp.setCode((String) tmpMap.get("code"));
				resp.setName((String) tmpMap.get("name"));
				resp.setProfile((String) tmpMap.get("profile"));
				resp.setRelCode((String) tmpMap.get("relCode"));
				resp.setRelCodehd((String) tmpMap.get("relCodehd"));
				resp.setVotes((Integer) tmpMap.get("votes"));
				tmpStr = (String) tmpMap.get("announceTime");
				if (!StringUtils.isBlank(tmpStr)){
					SimpleDateFormat tmpFormat1 = new SimpleDateFormat("yyyy-MM-dd");
					SimpleDateFormat tmpFormat2 = new SimpleDateFormat("M.d");
					resp.setAnnounceTime(tmpStr);
					resp.setAnnounceTimeFormat(tmpFormat2.format(tmpFormat1.parse(tmpStr)));
				}
				resp.setGameSite((String) tmpMap.get("gameSite"));
				resp.setAnnounceStatus((Integer) tmpMap.get("announceStatus"));
				tmpBig = (BigInteger) tmpMap.get("regionId");
				if (tmpBig != null){
					tmpSql = "select o.CODE as code,o.NAME as name from t_vote_region o where o.ID=" + tmpBig.longValue();
					tmpListRgMap = entityDao.getScrollDataBySql(tmpSql, null, -1, -1);
					if (tmpListRgMap != null && !tmpListRgMap.isEmpty()){
						tmpRgMap = tmpListRgMap.get(0);
						resp.setRegionCode((String) tmpRgMap.get("code"));
						resp.setRegionName((String) tmpRgMap.get("name"));
					}
				}
				resp.setPlayerNo((String) tmpMap.get("playerNo"));
				resp.setStatus((Integer) tmpMap.get("status"));
				logger.info("搜索冠军信息成功");
			}
			
		} catch (Exception e) {
			logger.error("程序异常，" + e.getMessage(), e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("程序异常");
		}
		return resp;
	}

	public GetVotePlayerListResp searchVotePlayerList(String voteCode,
			String playerNo, String sortName, String sortOrder, int type, int firstIndex, int maxResult) {
		
		GetVotePlayerListResp resp = new GetVotePlayerListResp();
		String resultDesc = String.format("搜索选手列表失败，投票编码[%s]非法！", voteCode);
		if (StringUtils.isBlank(voteCode)){
			logger.error(resultDesc);
			resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc(resultDesc);
			return resp;
		}
		if (StringUtils.isBlank(playerNo)){
			resultDesc = String.format("搜索选手列表失败，选手编码[%s]非法！", playerNo);
			logger.error(resultDesc);
			resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc(resultDesc);
			return resp;
		}
		String tmpSortName = "o.VOTES";
		String tmpSortOrder = "desc";
		if (!StringUtils.isBlank(sortName)){
			if (sortName.equals("createTime")){
				tmpSortName = "o.CREATE_TIME";
			}else if(sortName.equals("playerNo")){
				tmpSortName = "o.PLAYER_NO";
			}
		}
		if (!StringUtils.isBlank(sortOrder)){
			if (sortOrder.equals("asc")){
				tmpSortOrder = "asc";
			}
		}
		String tmpOrderBy = "";
		if (type == 0){
			if(sortName.equals("sequence")){
				tmpSortName = "o.SEQUENCE";
			}
			tmpOrderBy = " order by "+ tmpSortName + " " + tmpSortOrder;
		}else{
			tmpOrderBy = " order by "+ tmpSortName + " " + tmpSortOrder;
		}
		
		try {
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("搜索选手列表失败，投票编码[%s]不存在！", voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.VOTE_DOES_NOT_EXIST);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			
			List<GetVotePlayerListResp.VotePlayerInfo> tmpListVotePlayerInfo;
			GetVotePlayerListResp.VotePlayerInfo tmpVotePlayerInfo;
			TVoteRegion tmpTVoteRegion;
			List<Map<String, Object>> tmpListMap;
			List<Map<String, Object>> tmpListRgMap;
			Map<String, Object> tmpRgMap;
			Map<Long, TVoteRegion> tmpVrMap;
			BigInteger tmpBig;
			Long tmpRegionId;
			String tmpSql;
			
			//获取选手信息
			if (type == 0){
				tmpSql = "select count(o.id) as count from TVotePlayer o where o.tvoteGame.code=? and instr(o.playerNo,?)>0";
				List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{voteCode, playerNo}, -1, -1);
				int tmpIntCount = 0;
				if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
					Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
					Long tmpLongCount = (Long) tmpMapCount.get("count");
					tmpIntCount = tmpLongCount.intValue();
				}
				if (tmpIntCount <= 0) return resp;
				
				tmpSql = getVotePlayerSelectSql() + " where o.VOTE_ID=" + tmpTVoteGame.getId() + " and instr(o.PLAYER_NO,?)>0 " + tmpOrderBy;
				tmpListMap = entityDao.getScrollDataBySql(tmpSql, new Object[]{ playerNo}, firstIndex, maxResult);
				if (tmpListMap == null || tmpListMap.isEmpty()){
					return resp;
				}
				tmpListVotePlayerInfo = new ArrayList<GetVotePlayerListResp.VotePlayerInfo>();
				String tmpStr;
				tmpSql = "select o.CODE as code,o.NAME as name from t_vote_region o where o.ID=%s";
				tmpVrMap = new HashMap<Long, TVoteRegion>();
				for (Map<String, Object> tmpMap : tmpListMap){
					tmpVotePlayerInfo = new GetVotePlayerListResp.VotePlayerInfo();
					tmpStr = (String) tmpMap.get("avatar");
					if (!StringUtils.isBlank(tmpStr)){
						tmpVotePlayerInfo.setAvatar(getHttpUrl(tmpStr));
					}
					tmpStr = (String) tmpMap.get("avatarhd");
					if (!StringUtils.isBlank(tmpStr)){
						tmpVotePlayerInfo.setAvatarhd(getHttpUrl(tmpStr));
					}
					tmpVotePlayerInfo.setCode((String) tmpMap.get("code"));
					tmpVotePlayerInfo.setName((String) tmpMap.get("name"));
					tmpVotePlayerInfo.setProfile((String) tmpMap.get("profile"));
					tmpVotePlayerInfo.setRelCode((String) tmpMap.get("relCode"));
					tmpVotePlayerInfo.setRelCodehd((String) tmpMap.get("relCodehd"));
					tmpVotePlayerInfo.setVotes((Integer) tmpMap.get("votes"));
					tmpVotePlayerInfo.setVotesOther((Integer) tmpMap.get("votesOther"));
					tmpVotePlayerInfo.setVotesTotal((Integer) tmpMap.get("votesTotal"));
					tmpBig = (BigInteger) tmpMap.get("regionId");
					if (tmpBig != null){
						tmpRegionId = tmpBig.longValue();
						tmpTVoteRegion = tmpVrMap.get(tmpRegionId);
						if (tmpTVoteRegion == null){
							tmpListRgMap = entityDao.getScrollDataBySql(String.format(tmpSql, tmpRegionId), null, -1, -1);
							if (tmpListRgMap != null && !tmpListRgMap.isEmpty()){
								tmpRgMap = tmpListRgMap.get(0);
								tmpTVoteRegion = new TVoteRegion();
								tmpTVoteRegion.setCode((String) tmpRgMap.get("code"));
								tmpTVoteRegion.setName((String) tmpRgMap.get("name"));
								tmpVrMap.put(tmpRegionId, tmpTVoteRegion);
							}
						}
						if (tmpTVoteRegion != null){
							tmpVotePlayerInfo.setRegionCode(tmpTVoteRegion.getCode());
							tmpVotePlayerInfo.setRegionName(tmpTVoteRegion.getName());
						}
					}
					tmpVotePlayerInfo.setPlayerNo((String) tmpMap.get("playerNo"));
					tmpVotePlayerInfo.setStatus((Integer) tmpMap.get("status"));
					tmpVotePlayerInfo.setStartTime(DateFormatUtil.format((Date) tmpMap.get("startTime")));
					tmpVotePlayerInfo.setEndTime(DateFormatUtil.format((Date) tmpMap.get("endTime")));
					tmpListVotePlayerInfo.add(tmpVotePlayerInfo);
				}
				resp.setVotePlayerInfos(tmpListVotePlayerInfo);
				resp.setRecordSize(tmpIntCount);
				logger.info("搜索选手列表成功");
			//获取冠军信息
			}else {
				tmpSql = "select count(o.id) as count from TVoteWeekChamp o where o.tvoteGame.code=? and instr(o.playerNo,?)>0";
				List<Map<String, Object>> tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, new Object[]{voteCode, playerNo}, -1, -1);
				int tmpIntCount = 0;
				if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
					Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
					Long tmpLongCount = (Long) tmpMapCount.get("count");
					tmpIntCount = tmpLongCount.intValue();
				}
				if (tmpIntCount <= 0) return resp;
				
				tmpSql = getVoteWeekChampSelectSql() + " where o.VOTE_ID=" + tmpTVoteGame.getId() + " and instr(o.PLAYER_NO,?)>0 " + tmpOrderBy;
				tmpListMap = entityDao.getScrollDataBySql(tmpSql, new Object[]{ playerNo}, firstIndex, maxResult);
				if (tmpListMap == null || tmpListMap.isEmpty()){
					return resp;
				}
				tmpListVotePlayerInfo = new ArrayList<GetVotePlayerListResp.VotePlayerInfo>();
				String tmpStr;
				tmpSql = "select o.CODE as code,o.NAME as name from t_vote_region o where o.ID=%s";
				tmpVrMap = new HashMap<Long, TVoteRegion>();
				SimpleDateFormat tmpFormat1 = new SimpleDateFormat("yyyy-MM-dd");
				SimpleDateFormat tmpFormat2 = new SimpleDateFormat("M.d");
				for (Map<String, Object> tmpMap : tmpListMap){
					tmpVotePlayerInfo = new GetVotePlayerListResp.VotePlayerInfo();
					tmpStr = (String) tmpMap.get("avatar");
					if (!StringUtils.isBlank(tmpStr)){
						tmpVotePlayerInfo.setAvatar(getHttpUrl(tmpStr));
					}
					tmpStr = (String) tmpMap.get("avatarhd");
					if (!StringUtils.isBlank(tmpStr)){
						tmpVotePlayerInfo.setAvatarhd(getHttpUrl(tmpStr));
					}
					tmpVotePlayerInfo.setCode((String) tmpMap.get("code"));
					tmpVotePlayerInfo.setName((String) tmpMap.get("name"));
					tmpVotePlayerInfo.setProfile((String) tmpMap.get("profile"));
					tmpVotePlayerInfo.setRelCode((String) tmpMap.get("relCode"));
					tmpVotePlayerInfo.setRelCodehd((String) tmpMap.get("relCodehd"));
					tmpVotePlayerInfo.setVotes((Integer) tmpMap.get("votes"));
					tmpStr = (String) tmpMap.get("announceTime");
					if (!StringUtils.isBlank(tmpStr)){
						tmpVotePlayerInfo.setAnnounceTime(tmpStr);
						tmpVotePlayerInfo.setAnnounceTimeFormat(tmpFormat2.format(tmpFormat1.parse(tmpStr)));
					}
					tmpVotePlayerInfo.setGameSite((String) tmpMap.get("gameSite"));
					tmpVotePlayerInfo.setAnnounceStatus((Integer) tmpMap.get("announceStatus"));
					tmpBig = (BigInteger) tmpMap.get("regionId");
					if (tmpBig != null){
						tmpRegionId = tmpBig.longValue();
						tmpTVoteRegion = tmpVrMap.get(tmpRegionId);
						if (tmpTVoteRegion == null){
							tmpListRgMap = entityDao.getScrollDataBySql(String.format(tmpSql, tmpRegionId), null, -1, -1);
							if (tmpListRgMap != null && !tmpListRgMap.isEmpty()){
								tmpRgMap = tmpListRgMap.get(0);
								tmpTVoteRegion = new TVoteRegion();
								tmpTVoteRegion.setCode((String) tmpRgMap.get("code"));
								tmpTVoteRegion.setName((String) tmpRgMap.get("name"));
								tmpVrMap.put(tmpRegionId, tmpTVoteRegion);
							}
						}
						if (tmpTVoteRegion != null){
							tmpVotePlayerInfo.setRegionCode(tmpTVoteRegion.getCode());
							tmpVotePlayerInfo.setRegionName(tmpTVoteRegion.getName());
						}
					}
					tmpVotePlayerInfo.setPlayerNo((String) tmpMap.get("playerNo"));
					tmpVotePlayerInfo.setStatus((Integer) tmpMap.get("status"));
					tmpListVotePlayerInfo.add(tmpVotePlayerInfo);
				}
				
				resp.setVotePlayerInfos(tmpListVotePlayerInfo);
				resp.setRecordSize(tmpIntCount);
				logger.info("搜索冠军列表成功");
			}
		} catch (Exception e) {
			logger.error("程序异常，" + e.getMessage(), e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("程序异常");
		}
		return resp;
	}

	public GetVoteResp getVote(String voteCode) {
		GetVoteResp resp = new GetVoteResp();
		if (StringUtils.isBlank(voteCode)){
			logger.error("投票活动编码非法！");
			resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("投票活动编码非法！");
			return resp;
		}
		try {
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				logger.error("投票活动不存在，编码非法！");
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc("投票活动不存在，编码非法！");
				return resp;
			}
			resp.setCpCode(tmpTVoteGame.getCpCode());
			resp.setCode(voteCode);
			resp.setEndTime(DateFormatUtil.format(tmpTVoteGame.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
			resp.setName(tmpTVoteGame.getName());
			resp.setRule(tmpTVoteGame.getRule());
			resp.setStartTime(DateFormatUtil.format(tmpTVoteGame.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
			resp.setStatus(tmpTVoteGame.getStatus());
			resp.setVipVoteBase(tmpTVoteGame.getVipVoteBase());
			resp.setVoteBase(tmpTVoteGame.getVoteBase());
			resp.setVoteTimes(tmpTVoteGame.getVoteTimes());
			resp.setVoteTimes4k(tmpTVoteGame.getVoteTimes4k());
			resp.setVoteTimesHd(tmpTVoteGame.getVoteTimesHd());
			resp.setVoteTimesValidity(tmpTVoteGame.getVoteTimesValidity());
			logger.info("获取投票活动信息成功");
		} catch (Exception e) {
			logger.error("程序异常，" + e.getMessage(), e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("程序异常");
		}
		return resp;
	}

	public GetVoteListResp getVoteList(Long cpCode) {
		GetVoteListResp resp = new GetVoteListResp();
		if (cpCode == null){
			logger.error("cpCode非法！");
			resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
			resp.setResultDesc("cpCode非法！");
			return resp;
		}
		String tmpWhereJpql = null;
		if (cpCode != -1){
			tmpWhereJpql = "o.cpCode=" +cpCode;
		}
		try {
			List<TVoteGame> tmpListTVoteGame = entityDao.getScrollData(TVoteGame.class, tmpWhereJpql, null);
			if (tmpListTVoteGame == null || tmpListTVoteGame.isEmpty()){
				return resp;
			}
			List<GetVoteListResp.VoteInfo> tmpListVoteInfo = new ArrayList<GetVoteListResp.VoteInfo>();
			GetVoteListResp.VoteInfo tmpVoteInfo;
			for (TVoteGame tmpTVoteGame : tmpListTVoteGame){
				tmpVoteInfo = new GetVoteListResp.VoteInfo();
				tmpVoteInfo.setCpCode(tmpTVoteGame.getCpCode());
				tmpVoteInfo.setCode(tmpTVoteGame.getCode());
				tmpVoteInfo.setEndTime(DateFormatUtil.format(tmpTVoteGame.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
				tmpVoteInfo.setName(tmpTVoteGame.getName());
				tmpVoteInfo.setRule(tmpTVoteGame.getRule());
				tmpVoteInfo.setStartTime(DateFormatUtil.format(tmpTVoteGame.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
				tmpVoteInfo.setStatus(tmpTVoteGame.getStatus());
				tmpVoteInfo.setVipVoteBase(tmpTVoteGame.getVipVoteBase());
				tmpVoteInfo.setVoteBase(tmpTVoteGame.getVoteBase());
				tmpVoteInfo.setVoteTimes(tmpTVoteGame.getVoteTimes());
				tmpVoteInfo.setVoteTimes4k(tmpTVoteGame.getVoteTimes4k());
				tmpVoteInfo.setVoteTimesHd(tmpTVoteGame.getVoteTimesHd());
				tmpVoteInfo.setVoteTimesValidity(tmpTVoteGame.getVoteTimesValidity());
				tmpListVoteInfo.add(tmpVoteInfo);
			}
			resp.setVoteInfos(tmpListVoteInfo);
			resp.setRecordSize(tmpListVoteInfo.size());
			logger.info("获取投票活动列表成功");
		} catch (Exception e) {
			logger.error("程序异常，" + e.getMessage(), e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc("程序异常");
		}
		return resp;
	}

	public GetVoteRecordListResp getVoteRecordList(String voteCode, String playerCode, String userID, String sortName,
			String sortOrder, int firstIndex, int maxResult) {
		GetVoteRecordListResp resp = new GetVoteRecordListResp();
		String resultDesc = String.format("投票活动[%s]获取投票记录成功！", voteCode);
		try{
			if (StringUtils.isBlank(voteCode)){
				resultDesc = String.format("投票活动[%s]获取投票记录失败，编码非法！", voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.PARAMETER_IS_NOT_LEGAL);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			TVoteGame tmpTVoteGame = entityDao.get(TVoteGame.class, "o.code=?1", new Object[]{voteCode});
			if (tmpTVoteGame == null){
				resultDesc = String.format("投票活动[%s]获取投票记录失败，投票活动不存在！", voteCode);
				logger.error(resultDesc);
				resp.setResultCode(LuckyVoteResultCode.VOTE_DOES_NOT_EXIST);
				resp.setResultDesc(resultDesc);
				return resp;
			}
			
			String tmpSql = "select count(o.id) as count from TVoteRecord o where o.tvoteGame.id=" + tmpTVoteGame.getId();
			String tmpWhereSql = " where o.VOTE_ID=" + tmpTVoteGame.getId();
			String tmpParams = "";
			List<Map<String, Object>> tmpListMapCount;
			if (!StringUtils.isBlank(playerCode)){
				tmpSql = tmpSql + " and o.playerCode=?";
				tmpParams = playerCode + ",";
				tmpWhereSql = tmpWhereSql + " and o.PLAYER_CODE=?";
			}
			if (!StringUtils.isBlank(userID)){
				tmpSql = tmpSql + " and o.userId=?";
				tmpParams = tmpParams + userID + ",";
				tmpWhereSql = tmpWhereSql + " and o.USER_ID=?";
			}
			String[] tmpArrayParams = null;
			if (!StringUtils.isBlank(tmpParams)){
				tmpParams = StringUtils.removeEnd(tmpParams, ",");
				tmpArrayParams = tmpParams.split(",");
			}
			tmpListMapCount = entityDao.getScrollDataByHql(tmpSql, tmpArrayParams, -1, -1);
			int tmpIntCount = 0;
			if (tmpListMapCount != null && !tmpListMapCount.isEmpty()){
				Map<String, Object> tmpMapCount = tmpListMapCount.get(0);
				Long tmpLongCount = (Long) tmpMapCount.get("count");
				tmpIntCount = tmpLongCount.intValue();
			}
			if (tmpIntCount <= 0) return resp;
			
			tmpSql = "select o.PLAYER_CODE as playerCode,o.PLAYER_NAME as playerName,o.USER_ID as userId,o.VOTES as votes,"
					+ "o.CREATE_TIME as createTime from t_vote_record o";
			String tmpSortName = "o.CREATE_TIME";
			String tmpSortOrder = "desc";
			if (!StringUtils.isBlank(sortName)){
				if (sortName.equals("userID")){
					tmpSortName = "o.USER_ID";
				}else if(sortName.equals("playerCode")){
					tmpSortName = "o.PLAYER_CODE";
				}
			}
			if (!StringUtils.isBlank(sortOrder)){
				if (sortOrder.equals("asc")){
					tmpSortOrder = "asc";
				}
			}
			String tmpOrderBy = " order by " + tmpSortName + " " + tmpSortOrder;
			tmpSql = tmpSql + tmpWhereSql + tmpOrderBy;
			List<Map<String, Object>> tmpListMap = entityDao.getScrollDataBySql(tmpSql, tmpArrayParams, firstIndex, maxResult);
			if (tmpListMap == null || tmpListMap.isEmpty()){
				return resp;
			}
			
			List<GetVoteRecordListResp.VoteRecordInfo> tmpListVoteRecordInfo = new ArrayList<GetVoteRecordListResp.VoteRecordInfo>();
			GetVoteRecordListResp.VoteRecordInfo tmpVoteRecordInfo;
			Date tmpTime;
			for (Map<String, Object> tmpMap : tmpListMap){
				tmpVoteRecordInfo = new GetVoteRecordListResp.VoteRecordInfo();
				tmpVoteRecordInfo.setPlayerCode((String)tmpMap.get("playerCode"));
				tmpVoteRecordInfo.setPlayerName((String)tmpMap.get("playerName"));
				tmpVoteRecordInfo.setUserID((String)tmpMap.get("userId"));
				tmpVoteRecordInfo.setVotes((Integer)tmpMap.get("votes"));
				tmpTime = (Date)tmpMap.get("createTime");
				if (tmpTime != null){
					tmpVoteRecordInfo.setVoteTime(DateFormatUtil.format(tmpTime));
				}
				tmpListVoteRecordInfo.add(tmpVoteRecordInfo);
			}
			resp.setVoteRecordInfos(tmpListVoteRecordInfo);
			resp.setRecordSize(tmpIntCount);
			logger.info(resultDesc);
		}catch(Exception e){
			resultDesc = String.format("投票活动[%s]获取投票记录失败，程序异常！", voteCode);
			logger.error(resultDesc, e);
			resp.setResultCode(LuckyVoteResultCode.APPLICATION_EXCEPTION);
			resp.setResultDesc(resultDesc);
		}
		return resp;
	}

}